Last active
November 7, 2016 10:48
-
-
Save kyungw00k/046c7f739d7e8ae73b82c5926dfff989 to your computer and use it in GitHub Desktop.
handle onload/onfail callback on script tag
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 (src, cb) { | |
var userAgent = navigator.userAgent.toLowerCase() | |
var isIE = (userAgent.indexOf('msie') !== -1) ? parseInt(userAgent.split('msie')[1], 10) : false | |
var doc = document | |
var tag = 'script' | |
var el = doc.createElement(tag) | |
el.done = false | |
el.async = true | |
el.charset = 'utf-8' | |
if (isIE && isIE < 9) { | |
el.onreadystatechange = function () { // use readyState for handling onload/onfail | |
if (!el.done && /loaded|complete/.test(this.readyState)) { | |
_handleIE() | |
} | |
} | |
} | |
el.onload = function () { | |
_handleOnload() | |
} | |
el.onerror = function (err) { | |
_handleOnError(err || new Error('failed load: ' + src)) | |
} | |
el.src = src | |
if (!isIE || isIE >= 9) { // non-IE or IE 9 above browser can request src when it comes to append to document | |
_appendTo(document.head || document.getElementsByTagName('head')[0]) | |
} | |
// use code from http://stackoverflow.com/a/18840568 | |
function _handleIE () { | |
if (el.readyState === 'complete') { | |
_appendTo(document.head || document.getElementsByTagName('head')[0]) | |
el.onreadystatechange = null | |
_handleOnload() | |
return | |
} | |
var firstState = el.readyState | |
// hack: calling 'children' property changes node's readyState from 'loaded' to complete | |
// (if script was loaded normally) or to 'loading' - if error detected | |
el.children | |
// error detected | |
if (el.readyState === 'loading' && firstState === 'loaded') { | |
// custom error code | |
_handleOnError(new Error('failed load: ' + src)) | |
} | |
} | |
function _handleOnload () { | |
el.done = true | |
cb() | |
_destroyTag() | |
} | |
function _handleOnError (err) { | |
cb(err) | |
_destroyTag() | |
} | |
function _destroyTag () { | |
// Handle memory leak in IE | |
el.onreadystatechange = null | |
el.onload = null | |
el.onerror = null | |
el.parentNode.removeChild(el) | |
} | |
function _appendTo (base) { | |
base.appendChild(el) | |
} | |
} | |
if (typeof module === 'object' && module.exports) { | |
module.exports = loadScript | |
} else { | |
exports.loadScript = loadScript | |
} |
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
/* global describe, it, require */ | |
var assert = require('assert') | |
var loadScript = require('./load-script') | |
describe('loadScript', function () { | |
it('should be failed', function (done) { | |
this.timeout(5 * 1000) | |
loadScript('http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.x/jquery.min.js', function (err) { | |
if (err) { | |
assert.notEqual(err, null) | |
done() | |
} | |
}) | |
}) | |
it('should be loaded', function (done) { | |
this.timeout(5 * 1000) | |
loadScript('http://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js', function (err) { | |
if (err) { | |
done(err) | |
return | |
} | |
assert.notEqual(window.jQuery, null) | |
done() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment