Javascript 如何在另一个 js 文件中包含 jquery.js?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5577771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include jquery.js in another js file?
提问by ajay
I want to include jquery.js in myjs.js file. I wrote the code below for this.
我想在 myjs.js 文件中包含 jquery.js。我为此编写了下面的代码。
var theNewScript=document.createElement("script");
theNewScript.type="text/javascript";
theNewScript.src="http://example.com/jquery.js";
document.getElementsByTagName("head")[0].appendChild(theNewScript);
$.get(myfile.php);
There shows an error on the 5th line that is '$ not defined'. I want to include jquery.js and then want to call $.get() function in myjs.js file. How can I do this? Please help me
在第 5 行显示了一个错误,即“$ 未定义”。我想包含 jquery.js,然后想在 myjs.js 文件中调用 $.get() 函数。我怎样才能做到这一点?请帮我
采纳答案by Salman A
Appending a script tag inside the document head programmatically does not necessarily mean that the script will be available immediately. You should wait for the browser to download that file, parse and execute it. Some browsers fire an onload
event for scripts in which you can hookup your logic. But this is not a cross-browser solution. I would rather "poll" for a specific symbol to become available, like this:
追加的文件头中的脚本标签编程并不一定意味着该脚本将可立即。您应该等待浏览器下载该文件,解析并执行它。一些浏览器onload
为脚本触发一个事件,您可以在其中连接逻辑。但这不是跨浏览器的解决方案。我宁愿“轮询”某个特定符号可用,如下所示:
var theNewScript = document.createElement("script");
theNewScript.type = "text/javascript";
theNewScript.src = "http://example.com/jquery.js";
document.getElementsByTagName("head")[0].appendChild(theNewScript);
// jQuery MAY OR MAY NOT be loaded at this stage
var waitForLoad = function () {
if (typeof jQuery != "undefined") {
$.get("myfile.php");
} else {
window.setTimeout(waitForLoad, 1000);
}
};
window.setTimeout(waitForLoad, 1000);
回答by m0sa
The problem is that the script doesn't load instantly, it takes some time for the script file to download into your page and execute (in case of jQuery to define $).
问题是脚本不会立即加载,脚本文件需要一些时间才能下载到您的页面并执行(在 jQuery 定义 $ 的情况下)。
I would recommend you to use HeadJS. then you can do:
我建议你使用HeadJS。那么你可以这样做:
head.js("/path/to/jQuery.js", function() {
$.get('myfile.php');
});
回答by Craig White
Simple answer, Dont. The jQuery file is very touchy to intruders so dont try. Joining other files into jQuery file will often cause errors in the JS console, PLUS jQuery isn't initialized until the file is loaded into main document.
简单的回答,不要。jQuery 文件对入侵者来说非常敏感,所以不要尝试。将其他文件加入 jQuery 文件通常会导致 JS 控制台出错,而且 jQuery 在文件加载到主文档之前不会初始化。
Sorry, scratch that. Didnt quite know what you were doing.
对不起,划掉那个。完全不知道你在做什么。
Try this:
尝试这个:
var s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = 'http://domain.com/jquery.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);
回答by Amr Elgarhy
I used this code before, and it worked:
我之前使用过这段代码,它有效:
var t=document;
var o=t.createElement('script');
o=t.standardCreateElement('script');
o.setAttribute('type','text/javascript');
o.setAttribute('src','http://www.example.com/js/jquery-1.3.2.js');
t.lastChild.firstChild.appendChild(o);