码迷,mamicode.com
首页 > 编程语言 > 详细

用JavaScript动态加载CSS和JS文件

时间:2017-12-28 11:54:00      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:ext   http   required   .com   function   com   name   head   nbsp   

函数封装:

var dynamicLoading = {
    css: function(path){
        if(!path || path.length === 0){
            throw new Error(‘argument "path" is required !‘);
        }
        var head = document.getElementsByTagName(‘head‘)[0];
        var link = document.createElement(‘link‘);
        link.href = path;
        link.rel = ‘stylesheet‘;
        link.type = ‘text/css‘;
        head.appendChild(link);
    },
    js: function(path){
        if(!path || path.length === 0){
            throw new Error(‘argument "path" is required !‘);
        }
        var head = document.getElementsByTagName(‘head‘)[0];
        var script = document.createElement(‘script‘);
        script.src = path;
        script.type = ‘text/javascript‘;
        head.appendChild(script);
    }
}

 

使用:

//动态加载 CSS 文件
dynamicLoading.css("test.css");

//动态加载 JS 文件
dynamicLoading.js("test.js");

 

摘自:http://www.jsmix.com/javascript/dynamic-loading-css-javascript.html

 

用JavaScript动态加载CSS和JS文件

标签:ext   http   required   .com   function   com   name   head   nbsp   

原文地址:https://www.cnblogs.com/LChenglong/p/8133902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!