构建js

(function(){
    //用于兼容构建的函数,当没有构建时也不致于未定义报错!
    window.__inline = window.__pkg = window.__uri = function(p){return p;}
    //生成ajax请求
    function createHttpRequest() {
        var xmlHttp;
        if (window.ActiveXObject) { 
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        else if (window.XMLHttpRequest) { 
            xmlHttp = new XMLHttpRequest(); 
        }
        return xmlHttp;
    }

    //通过ajax请求资源
    function ajaxResource(url, callback, index, needTag) {
        var xmlhttp = createHttpRequest();
        xmlhttp.onreadystatechange=function(e){
            if(this.readyState == 4) {
                if(this.status == 200) {
                    callback&&callback(this.responseText);
                    //通过重试成功的资源
                    if(index > 0) {
                        window.__SENDDEBUGLOG && window.__SENDDEBUGLOG('reload resource '+url+' success count:' + index, true, '9001999902');
                    }
                }
                else {
                    //重试一次
                    if(index < 2 && this.status != 404) {
                        index++;
                        url = url.replace('qian-img.tenpay.com', location.hostname);
                        ajaxResource(url, callback, index, needTag);
                    }
                    else {
                        callback&&callback();
                    }
                    window.__SENDDEBUGLOG && window.__SENDDEBUGLOG('load resource '+url+' error count:' + index + ' status:'+this.status, true, '9001999901');
                }
            }
        };
        xmlhttp.open("GET",url,true); 
        //缓存中没有,指定了需要TAG处理,则优先返回,并延时处理资源
        if(index === 0 && needTag) {
            //css延时请求,因为如果缓存中没有的话,会直接生成link标签保证加载顺序
            setTimeout(function(){
                xmlhttp.send(null); 
            }, 800);
        } 
        else {
            xmlhttp.send(null); 
        }                                    
    }

    //资源加载
    //needTag 为表示是否需要用tag标签来加载没有缓存的资源,影响是否提前调用回调
    function loadResource(url, callback, index, needTag) {
        try {
            index = index || 0;
            var key = url.replace(location.hostname, 'qian-img.tenpay.com');
            var contentkey = '/*' + key + '*/'; //这里是完整的url,带有md5
            var sindex = key.indexOf('?');
            if(sindex > -1) key = key.substr(0, sindex);
            var content = window.localStorage.getItem(key);//先从缓存中取 
            //如果缓存中的内容配区上url(通常url中带有版本信息)               
            if(content && content.indexOf(contentkey) === 0) {
                callback&&callback(content);
            }
            else {                    
                ajaxResource(url, function(content){
                    if(content) {
                        content = contentkey + '\r\n' + content;
                        window.localStorage.setItem(key, content);                      
                    }
                    //needTag如果为true,则不再重复callback
                    if(!needTag) callback&&callback(content);      
                }, 0, needTag);
                //缓存中没有,指定了需要TAG处理,则优先返回
                if(needTag) callback && callback();
            }
        }
        catch(e){
            callback && callback();
        }
    }

    //加载JS
    //needScript 如果缓存没有是否需要用script加载
    window.loadJS = function(url, callback, needScript) {
        if(typeof callback == 'boolean') {
            needScript = callback;
            callback = null;
        }
        //如果是数组,加载多个js
        if(typeof url == 'object' && url.length) {
            var rets = [];
            for(var i=0;i= url.length) {
                        callback && callback.apply(this, rets);
                    }
                }, needScript);
            }
            return;
        }
        var jsLoaded = false;
        loadResource(url, function(content) {
            if(jsLoaded) return;
            jsLoaded = true;
            if(content) {
                try{
                    eval(content);
                }
                catch(e){
                    window.__docWriteScript && window.__docWriteScript(url); 
                }
                callback && callback(1);
            }
            else {
                if(needScript) window.__docWriteScript && window.__docWriteScript(url);  
                callback && callback(0);
            }
        }, 0, needScript);
    }
    //加载CSS,neetLink表示是否需要用link标签在无缓存时优先加载
    window.loadCSS = function(url, callback, container, neetLink) {
        var cssLoaded = false;
        loadResource(url, function(content) {
            if(cssLoaded) return;//不重复加载
            if(content) {
                var s = document.createElement('style');
                s.innerHTML = s.textContent = content;
                switch(container) {                        
                    case 'body':
                    case 'box': {
                        var box = document.querySelector('[data-role="page"]');
                        if(box&&box.length) {
                            box = box[box.length];
                        }
                        else {
                            box = document.body;
                        }
                        if(box) {
                            box.appendChild(s);
                            break;
                        }                            
                    }
                    case 'head':
                    default: {
                        document.head.appendChild(s);
                        break;
                    }
                }
                callback && callback(1);
            }
            else {
                window.__docWriteLink && window.__docWriteLink(url); 
                callback && callback(0);
            }
            cssLoaded = true;
        }, 0, typeof neetLink=='undefined'?true:neetLink);
    }        
})(); 

你可能感兴趣的:(构建js)