Last active
August 29, 2015 14:10
-
-
Save stuwilli/307b40d3e9605525e165 to your computer and use it in GitHub Desktop.
Load external JS script
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
/** | |
* Import/include an additional script in the document head | |
* @param {string} src the source file | |
* @param {function} callback the callback function | |
*/ | |
function importScript(src, callback) { | |
var script = document.createElement('script'); | |
script.setAttribute('type', 'text/javascript'); | |
script.setAttribute('src', src); | |
if (typeof callback === 'function') { | |
script.onload = callback; | |
script.onreadystatechange = function() { | |
if (this.readyState == 'complete') { | |
callback(); | |
} | |
}; | |
} | |
document.head.appendChild(script); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment