zoukankan      html  css  js  c++  java
  • 动态添加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文件路径"});


  • 相关阅读:
    缓存读写策略
    支撑京东小程序的开发框架 「Taro」
    Zookeeper vs Etcd
    前端开发利器 Web Replay
    kafka 中 zookeeper 具体是做什么的?
    newSQL 到底是什么?
    zookeeper配置集群
    zookeeper配置文件说明
    ssh远程访问-提示密钥不安全
    Nodejs-log4js使用配置
  • 原文地址:https://www.cnblogs.com/allen-tech/p/4758234.html
Copyright © 2011-2022 走看看