• js 动态加载js 并执行


        function loadJS(url, success) {
                var domScript = document.createElement('script');
                domScript.src = url;
                success = success || function () {};
                domScript.onload = domScript.onreadystatechange = function () {
                    if (!this.readyState || 'loaded' === this.readyState || 'complete' === this.readyState) {
                        success();
                        this.onload = this.onreadystatechange = null;
                        this.parentNode.removeChild(this);
                    }
                }
                document.getElementsByTagName('head')[0].appendChild(domScript);
            }
            loadJS("1.js");
        </script>

    转自  http://yijiebuyi.com/blog/2b23b571a0d5c94047f7e9c3150ac25d.html

    !function loadJS(){
        var jsFile = [
            {name:'file1',path:'js/file1.js',active:false,load:false},
            {name:'file2',path:'js/file2.js',active:false,load:false},
            {name:'file3',path:'js/file3.js',active:false,load:false},
            {name:'file4',path:'js/file4.js',active:false,load:false},
            {name:'file5',path:'js/file5.js',active:false,load:false}
        ]
        jsFile.forEach(function(item, index){
            if(localStorage['file_' + item.name]){
                item.load = true
                implementJS(item, index)
            }else{
                $.ajax({
                    type:"get",
                    url:item.path,
                    dataType:'text',
                    success:function(data){
                        item.content = data
                        item.load = true
                        implementJS(item, index)
                    }
                });
            }
        })
    
        function implementJS(item, index){
            //如果上一个文件已经执行了,则执行这个js文件
            if(index == 0 || jsFile[index - 1].active){
                storageJS(item, index)
                //尝试执行下一个js文件
                jsFile[index + 1] && jsFile[index + 1].load && implementJS(jsFile[index + 1], index + 1)
            }
        }
        function storageJS(item, index){
                //存储并执行js文件
                var name = 'file_' + item.name
                localStorage[name] = item.content || localStorage[name] || ''
                //这里要使用window.eval或者eval.call(window),否则eval里面的变量就不是全局变量
                window.eval(localStorage[name])
                item.active = true
        }
    }()

    //转自  https://segmentfault.com/a/1190000017548171

  • 相关阅读:
    sqlserver sql优化案例及思路
    mysql执行计划常用说明
    MYSQL 的rownum
    innodb crash
    spring-mybatis源码追踪
    mylyn提交到JIRA的日期格式错误
    [google面试CTCI] 2-1.移除链表中重复元素
    [google面试CTCI] 2-0.链表的创建
    [google面试CTCI] 1-8.判断子字符串
    [google面试CTCI] 1-7.将矩阵中特定行、列置0
  • 原文地址:https://www.cnblogs.com/enych/p/11805189.html
Copyright © 2020-2023  润新知