Home > Web Front-end > HTML Tutorial > 动态添加JS文件到页面_html/css_WEB-ITnose

动态添加JS文件到页面_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:38:03
Original
964 people have browsed it

 

/***** 功能:  加载外部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)            }        };    }}//调用方式
Copy after login
utools.getScript({url:"js文件路径"});
Copy after login
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template