Last active
August 29, 2015 14:03
-
-
Save wmakeev/da0e78318e7fa2259a4b to your computer and use it in GitHub Desktop.
Dynamic script load
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
var client_script = document.createElement('script'); | |
client_script.setAttribute('src','https://rawgit.com/wmakeev/moysklad-client/master/build/browser/moysklad-client.js'); | |
document.head.appendChild(client_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
var loadScript = function (src, cb) { | |
var head = document.head || document.getElementsByTagName('head')[0]; | |
var script = document.createElement('script'); | |
cb = cb || function() {}; | |
script.type = 'text/javascript'; | |
script.charset = 'utf8'; | |
script.async = true; | |
script.src = src; | |
stdOnEnd(script, cb); | |
head.appendChild(script) | |
}; | |
var stdOnEnd = function (script, cb) { | |
script.onload = function () { | |
this.onerror = this.onload = null; | |
cb() | |
}; | |
script.onerror = function () { | |
// this.onload = null here is necessary | |
// because even IE9 works not like others | |
this.onerror = this.onload = null; | |
cb(new Error('Failed to load ' + this.src)) | |
} | |
}; |
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
var loadScript = function (src, globalName) { | |
return new Promise(function(resolve, reject) { | |
if (globalName && window[globalName]) return resolve(); | |
var head = document.head || document.getElementsByTagName('head')[0]; | |
var script = document.createElement('script'); | |
script.type = 'text/javascript'; | |
script.charset = 'utf8'; | |
script.async = true; | |
script.src = src; | |
script.onload = function () { | |
this.onerror = this.onload = null; | |
console.debug('load-script: [' + (globalName || src) + '] injected'); | |
resolve(); | |
}; | |
script.onerror = function () { | |
// this.onload = null here is necessary | |
// because even IE9 works not like others | |
this.onerror = this.onload = null; | |
reject(new Error('Failed to load ' + this.src)) | |
}; | |
head.appendChild(script); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment