Home > Web Front-end > JS Tutorial > body text

How jquery dynamically loads js/css files

php中世界最好的语言
Release: 2018-04-23 14:41:11
Original
997 people have browsed it

jqueryThe built-in getSrcript file can only dynamically load js code, but cannot load css. Later, I wrote a program that can load js and css.

Let’s first look at jquery itself The getSrcript file with

Method

$.getScript(url,callback)
Copy after login

Instance

var testVar = 'New JS loaded!';
alert(testVar); function newFun(dynParam) {
alert('You just passed '+dynParam+ ' as parameter.');
}
Copy after login

Dynamic call method




Copy after login

The above can only dynamically load js code, but cannot load css. Later, I wrote a program that can load js and css.

$.extend({
include
Path: '',
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 + ">");
}
}
});
$.include(['hpbox.js','pop_win.css']);
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Recommended reading:

Jquery LigerUI detailed explanation of file upload steps

jquery dynamically loaded js file detailed explanation

The above is the detailed content of How jquery dynamically loads js/css files. For more information, please follow other related articles on the PHP Chinese website!

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 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!