This article mainly introduces how to dynamically load external Javascript files. This article provides an example to illustrate how to use the js loader to dynamically load external Javascript files, interested friends can refer to a js loader that can dynamically load js files found on the Internet recently. The specific code is as follows:
JsLoader. js
Var MiniSite = new Object ();/*** judge the 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 files */MiniSite. jsLoader = {/*** Load External js files * @ param sUrl the url of the js to be loaded * @ f Processing function */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') [0]. 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