Last active
August 29, 2015 14:11
-
-
Save dushmis/5b800e84aa3b31bc60f8 to your computer and use it in GitHub Desktop.
load js dynamically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function loadScript(path, callback) { | |
var script = document.createElement('script'); | |
if (script.onreadystatechange) { | |
script.onreadystatechange = function() { | |
var st = script.readyState; | |
if (st === 'loaded' || st === 'complete') { | |
if(callback&&"function" === typeof callback){ | |
callback(); | |
} | |
script.onreadystatechange = null; | |
} | |
}; | |
} else { | |
script.onload = function() { | |
callback(); | |
}; | |
} | |
script.src = path; | |
document.getElementsByTagName('head')[0].appendChild(script); | |
} | |
//loadscript-mini.js | |
//window.loadscript=function(d,b){var a=document.createElement("script");a.onreadystatechange?a.onreadystatechange=function(){var c=a.readyState;if("loaded"===c||"complete"===c)b&&"function"===typeof b&&b(),a.onreadystatechange=null}:a.onload=function(){b()};a.src=d;document.getElementsByTagName("head")[0].appendChild(a)}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment