Dynamic loading of external JavaScript files using the JS Loader

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.