星空网 > 软件开发 > 网页设计

动态添加JS文件到页面

 

/***** 功能: 加载外部JS文件,加载完成后执行回调函数callback***/var utools = {  config: {    id: "",    url: "",    charset: "gb2312",    callback: function () { }  },  merge: function (a, c) {    for (var b in c) a[b] = c[b];    return a  },  getScript: function (a) {    var r = Math.floor(Math.random() * 10000);    this.config = this.merge(this.config, a);    var callback = this.config.callback;    var scriptNode = document.createElement("script");    scriptNode.setAttribute("id", this.config.id);    scriptNode.setAttribute('charset', this.config.charset);    scriptNode.setAttribute('type', 'text/javascript');    scriptNode.setAttribute('src', this.config.url + "?r=" + r);    var head = document.getElementsByTagName("head")[0];    head.appendChild(scriptNode);    scriptNode[document.all ? "onreadystatechange" : "onload"] = function () {      if (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") {        if (callback) callback();        scriptNode.onreadystatechange = scriptNode.onload = null;        scriptNode.parentNode.removeChild(scriptNode)      }    };  }
}

//调用方式
utools.getScript({url:"js文件路径"});





原标题:动态添加JS文件到页面

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

5000 字说透私域玩法:https://www.kjdsnews.com/a/1550700.html
法国站卖家注意啦!从9月中旬开始,这项服务将开始收费!:https://www.kjdsnews.com/a/1550701.html
亚马逊大卖已入驻!跨境卖家疯狂涌向SHEIN:https://www.kjdsnews.com/a/1550702.html
注册VAT税号对跨境卖家有什么优势?哪些卖家需要注册VAT:https://www.kjdsnews.com/a/1550703.html
GmbH 和 UG,德国常见的公司形式:https://www.kjdsnews.com/a/1550704.html
独立站联盟营销比你想象中更有价值:https://www.kjdsnews.com/a/1550705.html
长治婚庆女司仪和主持人:https://www.vstour.cn/a/366176.html
北京丰台区水上乐园哪家好玩?:https://www.vstour.cn/a/366177.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流