• 移动端性能优化动态加载JS、CSS


    JS CODE

     (function() {
        /**
         * update:
         * 1.0
         */
        var version = "insure 1.1.0";
        var Zepto = Zepto || null, jQuery = jQuery || null, $ = Zepto || jQuery;
        var showLoading = false,
            isUsePackMode = false; // 是否使用合并模式,true则加载分页面合并的JS,CSS
        
        if (window.location.hostname == 'localhost') {
            isUsePackMode = false; //本地开发环境默认值为false
        }
        
        /**
         * 页面初始化, 动态加载
         */
        var location = window.location;
        // 协议
        var protocol = location.protocol;
        // 主机名
        var host = location.host;
        
        if (host.indexOf('pingan.com') != -1) {
            isUsePackMode = true;
        }
        // 加载一个脚本文件
        function _loadJsFile(url, callback) {
            var script = document.createElement("script");
            if(script.readyState) {
                script.onreadystatechange = function() {
                    if(script.readyState == "loaded" || script.readyState == "complete") {
                        callback.call();
                    }
                }
            } else {
                script.onload = callback;
            }
            script.type = "text/javascript";
            //script.async = true;
            script.src = url;
            //url.indexOf('?') != -1 ? url + '&v=' + version : url + '?v=' + version;
            document.getElementsByTagName("head")[0].appendChild(script);
        }
    
        // 加载脚本文件列表
        function _loadJsList(urls, statechange, _index) {
            var index = _index || 0;
            if(urls[index]) {
                _loadJsFile(urls[index], function() {
                    _loadJsList(urls, statechange, index + 1);
                });
            }
    
            if(statechange) {
                statechange(index);
            }
        }
    
        // 根据域名解析文件url
        function _parse(urls, type) {
            var _urls = [], url = "";
    
            if( typeof urls == "string") {
                urls = [urls];
            }
    
            for(var i = 0, len = urls.length; i < len; i++) {
                url = urls[i];
                if(!url) {
                    continue;
                } else if(/^(http|https)/.test(url)) {// 完整的URL
                    _urls.push(url);
                } else if(/^//.test(url)) {// 以根目录为路径
                    _urls.push(protocol + "//" + host + url);
                } else {
                    _urls.push(url+'?_='+Math.random());
                }
            }
    
            return _urls;
        }
    
        // 加载一个css文件
        function loadCSS(urls, packCss) {
            var html = [];
            urls = _parse(urls, "css");
            if (isUsePackMode && packCss) {
                urls = _parse(packCss, "css");
            }
            for(var i = 0, len = urls.length; i < len; i++) {
                html.push("<link type="text/css" rel="stylesheet" href="" + urls[i] + "" />");
            }
            document.write(html.join(""));
        }
    
        // 加载脚本文件
        function loadJs(urls, callback, dontevent, showLoad, packJs) {
            showLoading = showLoad;
            urls = _parse(urls, "js");
            if(typeof(_) != 'function' && typeof(jQuery) != 'function') {        
                urls.unshift(window.getDiffFrameUrl());
            }
            if (isUsePackMode && packJs) {
                packJs.unshift(window.getDiffFrameUrl());
                urls = _parse(packJs, "js");
            }
            if(!dontevent) {
                var _callback = callback, len = urls.length;
                callback = function(index) {
                    if(_callback) {
                        _callback(index);
                    }
                }
            }
            _loadJsList(urls, callback);
        }
    
        window.loadCSS = loadCSS;
        window.loadJs = loadJs;
        window.getDiffFrameUrl = function(prefix) {
            var b = navigator.userAgent.toLowerCase();
            browser = {
                safari : /webkit/.test(b),
                opera : /opera/.test(b),
                msie : /msie/.test(b) && !/opera/.test(b),
                mozilla : /mozilla/.test(b) && !/compatible/.test(b),
                winphone : window.navigator.msPointerEnabled
            };
            var prefix = prefix || '';
            //var url = prefix+'js/third-party/zepto.v1.1.3.min.js';
            var url = prefix+'/ebusiness/car/mobile/quoter/js/third-party/zepto.v1.1.3.js';
            if(browser.msie || browser.winphone) {
                url = prefix+'/ebusiness/car/mobile/quoter/js/third-party/jquery-1.9.1.min2.js';
            }
            return url;
        }
        
    })();

    调用方法

    <script type="text/javascript">
    $(function(){
        loadJs(['/ebusiness/car/mobile/quoter/js/common/common.js',
        '/ebusiness/car/mobile/quoter/js/common/city.js',
        '/ebusiness/car/mobile/quoter/js/common/date.js',
        '/ebusiness/car/mobile/quoter/js/insure/insureValidate.js',
        '/ebusiness/car/mobile/quoter/js/insure/insureBaseInfo.js'
        ],null,true,false);
    });
    </script>
  • 相关阅读:
    服务器性能调优(netstat监控大量ESTABLISHED连接与Time_Wait连接问题)
    maven/gredle配置阿里云仓库镜像,加速下载maven依赖
    Mac文件上传下载到服务器指定命令
    mysql-管理命令【创建用户、授权、修改密码、删除用户和授权、忘记root密码】
    springmvc返回不带引号的字符串
    maven过滤配置文件
    git常用命令/git 部分高级命令备忘录
    SpringBoot中使用Fastjson/Jackson对JSON序列化格式化输出的若干问题
    修改mysql配置中my.conf中max_allowed_packet变量
    ZK安装、ZK配置、ZK集群部署踩过的大坑
  • 原文地址:https://www.cnblogs.com/iicoo/p/5055169.html
Copyright © 2020-2023  润新知