Created
March 9, 2016 18:37
-
-
Save matijs/3bf8703a773b8b6a6ed0 to your computer and use it in GitHub Desktop.
Sure fire DOM Element insertion
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 insertScript(url, onload, onerror) { | |
var ref = document.getElementsByTagName('script')[0]; | |
var script = document.createElement('script'); | |
script.src = url; | |
if (typeof onload === 'function') { | |
script.onload = onload; | |
} | |
if (typeof onerror === 'function') { | |
script.onerror = onerror; | |
} | |
ref.parentNode.insertBefore(script, ref); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://www.paulirish.com/2011/surefire-dom-element-insertion/ for more info