2010-06-10 105 views

回答

52
var script = document.createElement("script"); 
script.src = "http://whatever.com/js/my/script.js"; 
document.body.appendChild(script); 
+1

HM OK,做了艰辛的道路。 – portoalet 2010-06-10 05:27:34

17

恰好碰到了同样的问题,以及 - 如jQuery是在项目中使用反正 - 我决定做一个简单:

$.getScript('example.js') 

或者:

$.getScript('example.js', function() { doSomethingOnceLoaded(); }) 

另外,我可以直接在Firebug中编写JavaScript,使用带有右侧文本输入窗格的控制台。用右下角的三角形按钮激活它。

+2

作为一名jQuery用户,我喜欢这个解决方案。我不知道getScript()。非常好。我也使用Firebug的jQuerify插件。这样,我就可以在没有它的页面上快速加载jQuery,然后使用您的解决方案加载额外的库。谢谢! – 2013-05-30 17:11:39

21

一个非常方便的命令是include();

include("http://code.jquery.com/jquery-latest.min.js"); 

了解更多关于include() command

+1

我试着在twitter的主页上的Firefox检查器中得到:'ReferenceError:include没有定义。' – isomorphismes 2013-06-22 00:53:52

+3

@isomorphismes因为这是Firebug控制台的特殊命令。 查看艾哈迈德提供的链接以及其他链接: https://getfirebug.com/wiki/index.php/Command_Line_API – fflorent 2013-07-12 19:14:47

+2

最适合我的解决方案。 – 2013-10-18 03:34:39