Last active
September 9, 2015 11:17
-
-
Save qizhihere/f07c438c4db2bae78faa to your computer and use it in GitHub Desktop.
load script with js
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
function loadScript(url, callback) { | |
// Adding the script tag to the head as suggested before | |
var head = document.getElementsByTagName('head')[0]; | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.src = url; | |
// Then bind the event to the callback function. | |
// There are several events for cross browser compatibility. | |
script.onreadystatechange = callback; | |
script.onload = callback; | |
// Fire the loading | |
head.appendChild(script); | |
} | |
function loadStyle(url, callback) { | |
// Adding the style tag to the head as suggested before | |
var head = document.getElementsByTagName('head')[0]; | |
var style = document.createElement('link'); | |
style.type = 'text/css'; | |
style.rel = 'stylesheet'; | |
style.href = url; | |
// Then bind the event to the callback function. | |
// There are several events for cross browser compatibility. | |
style.onreadystatechange = callback; | |
style.onload = callback; | |
// Fire the loading | |
head.appendChild(style); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment