Let's take a look at jquery's own Getsrcript file.
Method
The code is as follows |
Copy Code |
$.getscript (Url,callback) |
Instance
The code is as follows |
Copy Code |
var TestVar = ' New JS loaded! '; alert (TestVar); function Newfun (dynparam) { Alert (' You just passed ' +dynparam+ ' as parameter. '); } |
Dynamic Invocation Method
HTML code
code is as follows |
copy code |
<script type= "Text/javascript" src= ". /jquery.js "></script> <script type=" Text/javascript ">&NBSP; $ (function () { $ (' #loadButton '). Click (function () { $.getscript (' New.js ', function () { Newfun (') Checking new script "');//This function is inside the new.js, run this function } after clicking click; }); }); </script> <body> <button type= "button" id= "Loadbutton" >LOAD</BUTTON>&NBSP; |
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 |
$.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 ']); |