Home  >  Article  >  Web Front-end  >  jquery dynamic loading js/css file method (self-written small function)_jquery

jquery dynamic loading js/css file method (self-written small function)_jquery

WBOY
WBOYOriginal
2016-05-16 16:34:061443browse

Let’s first look at the getSrcript file that comes with jquery

Method

$.getScript(url,callback)

Example

Copy code The code is as follows:

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

Dynamic calling method

Copy code The code is as follows:



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

The code is as follows

Copy code The code is as follows:

$.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 ">");
}
}
});
$.include(['hdivbox.js','pop_win.css']);
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