Created
December 25, 2016 15:41
-
-
Save bh-lay/207ad75f2fd5e4a131b458a1fa9374dd to your computer and use it in GitHub Desktop.
简短的加载JS方法
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
| /** | |
| * 加载JS | |
| */ | |
| function LoadScript(url, callback){ | |
| var urls = typeof url === 'string' ? [url] : url; | |
| if (!Array.isArray(urls)){ | |
| throw 'argument type error'; | |
| return; | |
| } | |
| if (urls.length <= 0){ | |
| callback(); | |
| return; | |
| } | |
| var head = document.head; | |
| var script = document.createElement('script'); | |
| script.src = urls[0]; | |
| script.async = false; | |
| script.type = 'text/javascript' | |
| script.charset = 'utf-8' | |
| head.appendChild(script); | |
| script.onload = script.onerror = function(){ | |
| script.onload = script.onerror = null; | |
| LoadScript(urls.slice(1), callback); | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment