Home  >  Article  >  php教程  >  How to dynamically load js, css files using jquery

How to dynamically load js, css files using jquery

高洛峰
高洛峰Original
2016-11-24 09:47:521192browse

How to use jquery to dynamically load js, css files

$.extend({
includePath: ”,
include: function(file) {
var files = typeof file == ”string” ? [file]:file;
for (var i = 0; i < files.length; i++) {
var name = files[i].replace(/^\s|\s$/g, ”");
var att = name.split(‘.’);
var ext = att[att.length - 1].toLowerCase();
var isCSS = ext == ”css”;
var tag = isCSS ? ”link” : ”script”;
var attr = isCSS ? ” type=’text/css’ rel=’stylesheet’ ” : ” language=’javascript’ type=’text/javascript’ ”;
var link = (isCSS ? ”href” : ”src”) + ”=’” + $.includePath + name + ”‘”;
if ($(tag + ”[" + link + "]“).length == 0) document.write(“<” + tag + attr + link + ”></” + tag + ”>”);
}
}
});
 
 
//使用方法
$.includePath = ’http://hi.baidu.com/javascript/’;
$.include([&#39;json2.js&#39;, &#39;jquery.tree.js&#39;, &#39;jquery.tree.css&#39;]);


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn