我有一个标题可以运行一个js文件,可以包含在其他人的网站上,但是在IE8和IE9中,我得到:
'$'未定义
在控制台中。
我的代码只使用jQuery,如果是IE8或IE9,因为跨域问题需要使用jQuery库中的内置函数。在WordPress网站上浏览时,包含的脚本工作正常,但是在没有预先加载jQuery的另一个网站上,它不起作用,我在加载jQuery代码之前在头文件中包含jQuery的代码也不起作用。
var isIE = getInternetExplorerVersion();
if (isIE == 8 || isIE == 9) {
// Insert jQuery <script> to <head>.
var head = document.getElementsByTagName( 'head' )[0], script;
script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js';
head.insertBefore( script, head.lastChild );
// Run jQuery test
$('html').click(function(){
// Runs without issue on sites that ALREADY have jQuery loaded when IN IE.
alert('Clicked while in IE');
});
} else {
// Runs without issue when not on IE.
alert('Not in IE');
}
对于那些想知道的人来说,这是确定IE版本的功能(在这种情况下无关紧要):
function getInternetExplorerVersion() {
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer');
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
}
return rv;
}
答案 0 :(得分:1)
您不需要编写自定义代码来检查IE版本。 IE发布了这个&#34;功能&#34;(;
时的解决方案<!--[if lt IE 9]>
<script src="jquery-1.9.0.js"></script>
<![endif]-->
<!--[if gte IE 9]><!-->
<script src="jquery-2.0.0.js"></script>
<!--<![endif]-->
有关Internet Explorer here的条件注释的更多信息。
答案 1 :(得分:0)
首先,要直接解决您的问题,您将异步加载脚本 (读取:乱序)。因此,在您尝试加载jQuery之后的代码并不一定意味着在那时加载了jQuery。由于您创建了script
元素,因此可以附加onload
script.onload = function(){
// This will execute when the script you loaded is loaded
}
要回答另一个问题,为什么你要根据浏览器版本加载jQuery,我建议你使用最新的1.x.x版本的jQuery。它们与IE6 +以及更新的浏览器兼容。无需检查版本。
您的脚本可能已压缩为:
;(function(ready){
if(jQuery) return ready();
var script = document.createElement('script');
document.head.appendChild(script);
script.onload = ready;
script.src = "path/to/jquery.1.x.x.js";
}(function(){
// Code here runs when ready
}));
答案 2 :(得分:0)
首先,感谢您的所有答案。
虽然它们是有用的,但它们无法解决问题,因为在加载此页面之前jQuery必须存在于页面上,并且即使在加载此js文件之后将其注入头部也不会有效。
解决问题我在此之前创建了另一个js文件。在该文件中,它检查jQuery是否在页面上,浏览器是否与我们的xdomain脚本不兼容,如果是,则我们通过脚本添加jquery文件,然后添加带有我们所有功能的js文件。 / p>
文件1:
(function(ready) {
'use strict';
var isIE = getInternetExplorerVersion();
if ((isIE == 8 || isIE == 9) && window.jQuery) {
// On IE and jQuery exists
return ready;
} else if (isIE == 8 || isIE == 9) {
// On IE and jQuery doesn't exit
var head = document.getElementsByTagName( 'head' )[0];
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js';
script.onload = ready;
document.head.appendChild(script);
return ready;
} else {
// Not on IE
return ready;
}
script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = 'http://www.test.com/path/to/other/js_file.js';
script.async = true;
head.insertBefore( script, head.lastChild );
})();
function getInternetExplorerVersion() {
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
}
return rv;
}
文件2(上面的js_file.js):
(function(ready) {
var isIE = getInternetExplorerVersion();
if (isIE == 8 || isIE == 9) {
// Run jQuery test
$('html').click(function(){
// Runs without issue on sites that ALREADY have jQuery loaded when IN IE.
alert('Clicked while in IE');
});
} else {
// Runs without issue when not on IE.
alert('Not in IE');
}
})();