Home > php教程 > PHP开发 > body text

How to dynamically load js, css files using jquery

高洛峰
Release: 2016-11-24 09:47:52
Original
1169 people have browsed it

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;]);
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!