您的位置:首页 > Web前端 > CSS

自己用的一个动态加载js,css文件 预加载图片

2020-02-04 03:22 501 查看

//加载js,css 文件 预加载图片

var FileLoading = {

    isInclude:function(type,name){ //判断文件是否存在

        var es=document.getElementsByTagName(type);

        for(var i=0;i<es.length;i++)

        if(es[i][type == 'script'?'src':'href'].indexOf(name)!=-1)return true;

        return false;

    },

css: function(path,callback){ //加载css文件

        if(!path || path.length === 0){

            throw new Error('argument "path" is required !');

        }

        if(FileLoading.isInclude('link',path)){

            callback&&callback();

            return;

        }

        var head = document.getElementsByTagName('head')[0];

        var noLoad = true;

var link = document.createElement('link');

link.rel = 'stylesheet';

        link.type = 'text/css';

        link.onreadystatechange= function () {

            if (this.readyState == 'complete' || this.readyState == 'loaded'){

                noLoad&&callback&&callback();

                noLoad = false;

            }

        }

        link.οnlοad= function(){

            noLoad&&callback&&callback();

            noLoad = false;

        }

        link.href = path;

head.appendChild(link);

},

js: function(path,callback){ //加载js文件

        if(!path || path.length === 0){

            throw new Error('argument "path" is required !');

        }

        if(FileLoading.isInclude('script',path)){

            callback&&callback();

            return;

        }

        var noLoad = true;

        var head = document.getElementsByTagName('head')[0];

var script = document.createElement('script');

script.src = path;

script.type = 'text/javascript';

script.onreadystatechange= function () {

            if (this.readyState == 'complete' || this.readyState == 'loaded'){

                noLoad&&callback&&callback();

                noLoad = false;

}

        }

        script.οnlοad= function(){

            noLoad&&callback&&callback();

            noLoad = false;

            

        }

head.appendChild(script);

    },

    jsArr: function(path,callback){ //批量加载js文件

        var callNum = 0;

        var pathLen = path.length;

        for(var i=0;i<pathLen;i++){

            FileLoading.js(path[i],function(){

                callNum++;

                callNum>=pathLen&&callback&&callback();

            })

        }

    },

    cssArr: function(path,callback){ //批量加载css文件

        var callNum = 0;

        var pathLen = path.length;

        for(var i=0;i<pathLen;i++){

            FileLoading.css(path[i],function(){

                callNum++;

                callNum>=pathLen&&callback&&callback();

            })

        }

    },

    preload:function(path,as){ //预加载

        if(!path || path.length === 0){

            throw new Error('argument "path" is required !');

        }

        if(FileLoading.isInclude('link',path)){

            return;

        }

        var head = document.getElementsByTagName('head')[0];

var link = document.createElement('link');

link.rel = 'preload';

        link.as =   as;

        link.href = path;

head.appendChild(link);

    },

    preloadJs:function(path){ //预加载js文件

        FileLoading.preload(path,'script');

    },

    preloadJsArr:function(paths){ //预加载js文件

        for(var i in paths){

            FileLoading.preloadJs(paths[i]);

        }

    },

    preloadImage:function(path){ //预加载图片

        FileLoading.preload(path,'image');

    },

    preloadImageArr:function(paths){ //批量预加载图片

        for(var i in paths){

            FileLoading.preloadImage(paths[i]);

        }

    }

}

FileLoading.js('jsFileUrl',function(){alert('加载成功')});

FileLoading.css('jsFileUrl',function(){alert('加载成功')});

FileLoading.preloadJsArr(['jsFileUrl','jsFileUrl'],function(){alert('预加载成功')});

FileLoading.jsArr(['jsFileUrl','jsFileUrl'],function(){alert('加载成功')});

FileLoading.preloadImage(['imgFileUrl','imgFileUrl'],function(){alert('预加载成功')});

    

 

  • 点赞
  • 收藏
  • 分享
  • 文章举报
布从良 发布了1 篇原创文章 · 获赞 0 · 访问量 302 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: