Let's take a look at jquery's own Getsrcript file.
Method
$.getscript (Url,callback)
Instance
Copy Code code as follows:
var TestVar = ' New JS loaded! ';
alert (TestVar); function Newfun (dynparam) {
Alert (' You just passed ' +dynparam+ ' as parameter. ');
}
Dynamic Invocation Method
Copy Code code as follows:
<script type= "Text/javascript" src= ". /jquery.js "></script>
<script type= "Text/javascript" >
$ (function ()
{
$ (' #loadButton '). Click (function () {
$.getscript (' New.js ', function () {
Newfun (' Checking new script ');//This function is inside the new.js, run this function when clicked click
});
});
});
</script>
<body>
<button type= "button" id= "Loadbutton" >Load</button>
The above can only dynamically load JS code, but can not load CSS, and later wrote a can load JS and CSS program.
The code is as follows
Copy Code code 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 + "></" + tag + ">");
}
}
});
$.include ([' Hdivbox.js ', ' pop_win.css ']);