我试图找出用户是否支持jQuery 2.x
这工作正常,但是当我尝试运行脚本时,它不起作用,因为jQuery没有完成加载...jQuery版本选择完成后运行
jQuery完成加载后,如何触发__run()
。
初始化脚本:
function __run(){
//
// function runs the jQuery website
//
$("body").append("<p>Test</p>");
}
(function() {
var s, s0, js;
if (typeof JSON !== 'undefined' && 'querySelector' in document && 'addEventListener' in window) {
js = '//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js';
} else {
js = '//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js';
alert('Woow, that\'s one old browser, maybe you should upgrade. We don\'t support this version, you can try to use the website though');
}
s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = js;
s0 = document.getElementsByTagName('script')[0];
s0.parentNode.insertBefore(s, s0);
__run();
}());
你为什么用JavaScript这样做呢?使用条件注释来处理这个问题要容易得多,因为IE是唯一受影响的浏览器。 –
好问题,哈哈,因为这个原因实际上,有老浏览器的人更新他们的浏览器... :-) –