最近我在我的网站上添加了 facebook 点赞按钮、twitter 关注我们按钮和 google +1 按钮。我希望他们的 JS 脚本在我告诉他们加载时加载。
因此,我需要一个加载外部 JS 文件的函数。我不需要知道文件何时完成加载(不需要回调)。
我在 Internet 上找到了一些方法/功能,但我想知道哪种方法/功能最适合这种情况?
4 ways to dynamically load external JavaScript
Dynamically loading JS libraries and detecting when they're loaded
The best way to load external JavaScript
谢谢。
编辑: 添加了我找到的方法/功能。
最佳答案
我建议使用 jQuery 的 getScript()。对于 twitter 按钮,您将像这样加载相应的脚本:
$.getScript("//platform.twitter.com/widgets.js")
当然你必须load jquery首先在你的脚本中,不要忘记添加 markup needed for the twitter-button在你的 html 中。
关于JavaScript - 需要加载外部 JS 文件的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8278492/