Today I found a js loader that can dynamically load js files on the Internet. The specific code is as follows:
JsLoader.js
var MiniSite=new Object();/*** Judge browser*/MiniSite.Browser={ ie:/msie/.test(window.navigator.userAgent.toLowerCase()), moz:/gecko/.test(window.navigator.userAgent.toLowerCase()), opera:/opera/.test(window.navigator.userAgent.toLowerCase()), safari:/safari/.test(window.navigator.userAgent.toLowerCase()) };/*** JsLoader object is used to load external js file*/MiniSite.JsLoader={/*** Load external js file* @param sUrl The URL of the js to be loaded* @fCallback The processing function after js loading*/load:function(sUrl,fCallback){ var _script=document.createElement('script'); _script.setAttribute('charset','gbk'); _script.setAttribute('type','text/javascript'); _script.setAttribute('src',sUrl); document.getElementsByTagName('head')[].appendChild(_script); if(MiniSite.Browser.ie){ _script.onreadystatechange=function(){ if(this.readyState=='loaded'||this.readyStaate=='complete'){ //fCallback(); if(fCallback!=undefined){fCallback(); }} }; }else if(MiniSite.Browser.moz){ _script.onload=function(){ //fCallback(); if(fCallback!=undefined){fCallback(); }}; }else{ //fCallback();if(fCallback!=undefined){fCallback(); }} } };JsLoader.js test
<!DOCTYPE HTML><html><head><!--Introducing js loader--><script type="text/javascript" src="js/JsLoader.js"></script><title>JsLoaderTest.html</title><script type="text/javascript">if(MiniSite.Browser.ie){//Dynamic loading JsMiniSite.JsLoader.load("js/jquery-...js",function(){alert("Dynamic loading is jquery-...js");$(function(){alert("The processing operation done after the dynamic loading of jquery-...js");});}); }else{MiniSite.JsLoader.load("js/jquery-...js",function(){alert("Dynamic loading is jquery-...js");$(function(){alert("Jquery-...js processing operations done after dynamic loading of jquery-...js");});});});}</script></head><body></body></html>The test results are as follows: