[英]Include jQuery from another JavaScript file
I am trying to include jQuery from a javascript file. 我试图从javascript文件中包含jQuery。 I have tried the following, although it doesn't work.
我尝试了以下,虽然它不起作用。
var script = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></script>';
document.getElementsByTagName('head')[0].appendChild(script);
</script>
closes the opening <script>
block, even if it's in a string. </script>
关闭打开的<script>
块,即使它在字符串中。 I would do it this way: 我会这样做:
(function() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = document.location.protocol + '//code.jquery.com/jquery-1.9.1.min.js';
document.getElementsByTagName('head')[0].appendChild(script)
})();
You can't have </script>
anywhere inside a script block, not even inside a string, because it will end the script block there. 你不能在脚本块中的任何地方拥有
</script>
,甚至不能在字符串中,因为它将在那里结束脚本块。
Break up the ending tag in the string: 分解字符串中的结束标记:
var script = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"></scr'+'ipt>';
(function() {
var script = document.createElement('script');
script.type = "text/javascript"; // keeping older browsers happy.
script.src = window.location.protocol + '//code.jquery.com/jquery-1.9.1.min.js';
// browsers prevent cross-protocol downloading.
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);// In Opera a site can get by without a <head>
})();
Just use the jQuery getScript() method to load jQuery: http://api.jquery.com/jQuery.getScript/ 只需使用jQuery getScript()方法加载jQuery: http ://api.jquery.com/jQuery.getScript/
...Just kidding. ...开玩笑。
Try this code: 试试这段代码:
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'http://code.jquery.com/jquery-1.9.1.min.js';
head.appendChild(script);
From: http://unixpapa.com/js/dyna.html 来自: http : //unixpapa.com/js/dyna.html
Also, if using on an https page, you will need to load the script from an https compatible CDN, like the Google Hosted Libraries (src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js") 此外,如果在https页面上使用,则需要从兼容https的CDN加载脚本,例如Google Hosted Libraries(src =“// ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery .min.js“)
var script = document.createElement('script');
script.src = 'http://code.jquery.com/jquery-1.9.1.min.js';
document.getElementsByTagName('head')[0].appendChild(script);
using a tiny re-usable script adder: 使用一个微小的可重用脚本加法器:
function fetch(url){
var d=document, s='script';
d.getElementsByTagName(s)[0].parentNode.appendChild(d.createElement(s)).src=url;
}
fetch('//code.jquery.com/jquery-1.9.1.min.js');
not all pages have HEADs in all browsers, but if a script is running, so can a sibling script tag... 并非所有页面都在所有浏览器中都有HEAD,但如果脚本正在运行,兄弟脚本标签也可以运行......
First of all, the variable script contains the sequence </script>
which you can not make it appears as it is in your code, because browser will assume(and it must) that it is <script>
tag close. 首先,变量脚本包含序列
</script>
,您无法使其显示在代码中,因为浏览器将假定(并且必须)它是<script>
标记关闭。 for example if your script code contains syntax error, which is a string variable that has no close "
it will looks like 例如,如果您的脚本代码包含语法错误,这是一个没有关闭的字符串变量
"
它看起来像
<script>var bad = "abcd ;</script>
to solve this you can break the </script>
string like "</scr" + "ipt>"
or you could escape it: "<\\/script>"
要解决这个问题,你可以打破像
"</scr" + "ipt>"
这样的</script>
字符串,或者你可以将其转义: "<\\/script>"
so: 所以:
var script = '<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.min.js"><\/script>';
document.getElementsByTagName('head')[0].appendChild(script);
Second thing is that appendChild()
function accept a Node element and not a string 第二件事是
appendChild()
函数接受Node元素而不是字符串
so: 所以:
var script = document.createElement("script");
script.src = "http://code.jquery.com/jquery-1.9.1.min.js";
document.getElementsByTagName("head")[0].appendChild(script);
Anyway, I prefer to use a module and JavaScript loader like RequireJS . 无论如何,我更喜欢使用像RequireJS这样的模块和JavaScript加载器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.