js动态加载以及确定加载完成的代码
更新时间:2011年07月31日 23:53:35 作者:
利用js动态加载js文件到页面,并在确定加载完成后调用相关function
代码如下:
var otherJScipt = document.createElement("script");
otherJScipt = document.createElement("script");
otherJScipt.setAttribute("type", "text/javascript");
otherJScipt.setAttribute("src", "/xxx.js");
document.getElementsByTagName("head")[0].appendChild(otherJScipt);//追加到head标签内
//判断服务器
if (navigator.userAgent.indexOf("IE") >= 0) {
//IE下的事件
otherJScipt.onreadystatechange = function () {
//IE下的判断,判断是否加载完成
if (otherJScipt && (otherJScipt.readyState == "loaded" || otherJScipt.readyState == "complete")) {
otherJScipt.onreadystatechange = null;
callMyFun();
}
};
}
else {
otherJScipt.onload = function () {
otherJScipt.onload = null;
callMyFun();
};
}
复制代码 代码如下:
var otherJScipt = document.createElement("script");
otherJScipt = document.createElement("script");
otherJScipt.setAttribute("type", "text/javascript");
otherJScipt.setAttribute("src", "/xxx.js");
document.getElementsByTagName("head")[0].appendChild(otherJScipt);//追加到head标签内
//判断服务器
if (navigator.userAgent.indexOf("IE") >= 0) {
//IE下的事件
otherJScipt.onreadystatechange = function () {
//IE下的判断,判断是否加载完成
if (otherJScipt && (otherJScipt.readyState == "loaded" || otherJScipt.readyState == "complete")) {
otherJScipt.onreadystatechange = null;
callMyFun();
}
};
}
else {
otherJScipt.onload = function () {
otherJScipt.onload = null;
callMyFun();
};
}
相关文章
JavaScript 双位非运算(~~ 操作符)使用场景实例探索
本文为大家介绍JavaScript中双位非运算 ~~, ~~ 操作符是一个强大且经常被忽视的特性,它提供了一种快速、简洁的方式来处理数字和执行类型转换,通常可以被用于数学计算和类型转换,我们先了解一下 ~~ 的基本概念和它的一些应用场景2024-01-01
JavaScript hasOwnProperty() 函数实例详解
hasOwnProperty()函数用于指示一个对象自身(不包括原型链)是否具有指定名称的属性。下面通过本文给大家分享JavaScript hasOwnProperty() 函数实例讲解,感兴趣的朋友一起看看吧2017-08-08
解析javascript系统错误:-1072896658的解决办法
问题出现在用到ajax的场合。昨天还正常的程序,今天运行就有javascript系统错误:-1072896658的2013-07-07


最新评论