Today on the internet found a can dynamically load JS file JS loader, the specific code is as follows:
Jsloader.js
1 varMinisite=NewObject ();2 /**3 * Judging Browser4 */5Minisite.browser={ 6ie:/msie/. Test (Window.navigator.userAgent.toLowerCase ()),7moz:/gecko/. Test (Window.navigator.userAgent.toLowerCase ()),8opera:/opera/. Test (Window.navigator.userAgent.toLowerCase ()),9safari:/safari/. Test (Window.navigator.userAgent.toLowerCase ())Ten }; One /** A * Jsloader object is used to load external JS file - */ -Minisite.jsloader={ the /** - * Loading the external JS file - * @param surl The URL address of the JS to be loaded - * @fCallback JS Load After the completion of the processing function + */ -Loadfunction(surl,fcallback) { + var_script=document.createelement (' script '); A_script.setattribute (' CharSet ', ' GBK '); at_script.setattribute (' type ', ' Text/javascript '); -_script.setattribute (' src ', sURL); -document.getElementsByTagName (' head ') [0].appendchild (_script); - if(MiniSite.Browser.ie) { -_script.onreadystatechange=function(){ - if( This. readystate== ' Loaded ' | | This. readystaate== ' Complete '){ in //Fcallback (); - if(fcallback!=undefined) { to Fcallback (); + } - the } * }; $}Else if(MiniSite.Browser.moz) {Panax Notoginseng_script.onload=function(){ - //Fcallback (); the if(fcallback!=undefined) { + Fcallback (); A } the }; +}Else{ - //Fcallback (); $ if(fcallback!=undefined) { $ Fcallback (); - } - } the } -};
Jsloader.js Test
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <!--introduction of JS loader -5 <Scripttype= "Text/javascript"src= "Js/jsloader.js"></Script>6 <title>Jsloadertest.html</title>7 <Scripttype= "Text/javascript">8 if(MiniSite.Browser.ie) {9 //Dynamic Loading JSTen MiniSite.JsLoader.load ("Js/jquery-1.9.1.js",function(){ One Alert ("dynamically loaded is the Jquery-1.9.1.js"); A $(function(){ - Alert ("jquery-1.9.1.js processing operations after dynamic loading is complete"); - }); the }); - }Else{ - MiniSite.JsLoader.load ("Js/jquery-2.0.3.js",function(){ - Alert ("dynamically loaded is the Jquery-2.0.3.js"); + $(function(){ - Alert ("jquery-2.0.3.js processing operations after dynamic loading is complete"); + }); A }); at } - </Script> - </Head> - - <Body> - in </Body> - </HTML>
The test results are as follows:
Test results under IE browser:
Test results under Google Chrome: