zoukankan      html  css  js  c++  java
  • 网站动态加载JS脚本


    Demo_1

    
    function loadJS(url, fn) {
        var ss = document.getElementsByName('script'),
            loaded = false;
        for (var i = 0, len = ss.length; i < len; i++) {
            if (ss[i].src && ss[i].getAttribute('src') == url) {
                loaded = true;
                break;
            }
        }
        if (loaded) {
            if (fn && typeof fn != 'undefined' && fn instanceof Function) fn();
            return false;
        }
        var s = document.createElement('script'),
            b = false;
        s.setAttribute('type', 'text/javascript');
        s.setAttribute('src', url);
        s.onload = s.onreadystatechange = function () {
            if (!b && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
                b = true;
                if (fn && typeof fn != 'undefined' && fn instanceof Function) fn();
            }
        };
        document.getElementsByTagName('body')[0].appendChild(s);
    }
    
    


    Demo_2

    
    function JSLoad(args) {
        s = document.createElement("script");
        s.setAttribute("type", "text/javascript");
        s.setAttribute("src", args.url);
        s.onload = s.onreadystatechange = function () {
            if (!s.readyState || s.readyState == "loaded" || s.readyState == "complete") {
                if (typeof args.callback == "function") args.callback(this, args);
                s.onload = s.onreadystatechange = null;
                try {
                    s.parentNode && s.parentNode.removeChild(s);
                } catch (e) {}
            }
        };
        document.getElementsByTagName("body")[0].appendChild(s);
    }
    
    
  • 相关阅读:
    团队冲刺2.6
    团队冲刺2.5
    团队冲刺2.4
    团队冲刺2.3
    个人作业二
    个人作业二
    个人作业二
    课程总结
    每日博客
    每日博客
  • 原文地址:https://www.cnblogs.com/lalalagq/p/10207825.html
Copyright © 2011-2022 走看看