判断javascript加载完成的方法
function include_js(file) { var _doc = document.getElementsByTagName('head')[0]; var js = document.createElement('script'); js.setAttribute('type', 'text/javascript'); js.setAttribute('src', file); _doc.appendChild(js); if (!/*@cc_on!@*/0) { //if not IE //Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload js.onload = function () { alert('Firefox2、Firefox3、Safari3.1+、Opera9.6+ support js.onload'); } } else { //IE6、IE7 support js.onreadystatechange js.onreadystatechange = function () { if (js.readyState == 'loaded' || js.readyState == 'complete') { alert('IE6、IE7 support js.onreadystatechange'); } } } return false;}ie 上支持 onreadystatechange , 不支持 onload
firefox上支持 onload , 不支持onreadystatechange
所以jquery里面的解决方案如下
var done = false;
script.onload = script.onreadystatechange = function() {
if(!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
callback();
}
};
其中 ff onload上面没有readyState 这个值,所以用了 !this.readyState
ie上面不一定loaded 或者是 complete触发 , 或者两个都会触发,所以用一个变量进行了标记。
这样就兼容了浏览器上插入script标签的问题。

浙公网安备 33010602011771号