Created
July 14, 2017 02:27
-
-
Save ryanzhouff/8b895f925038fbd001434e75a1a848f2 to your computer and use it in GitHub Desktop.
动态加载脚本
This file contains 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
var loadScript = function (url, callback) { | |
var script = document.createElement("script"), | |
head = document.getElementsByTagName("head")[0]; | |
script.type = "text/javascript"; | |
// IE | |
if (script.readyState) { | |
script.onreadystatechange = function () { | |
if (script.readyState === "loaded" || script.readyState === "complete") { | |
script.onreadystatechange = null; | |
callback && callback(); | |
} | |
} | |
} else { | |
// Chrome, Firefox等 | |
script.onload = function () { | |
callback && callback(); | |
} | |
} | |
script.src = url; | |
head.appendChild(script); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment