Last active
February 3, 2017 13:13
-
-
Save englishextra/8dc9fe7b6ff8bdf5f9b483bf772b9e1c to your computer and use it in GitHub Desktop.
Load and execute JS via AJAX
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
/*! | |
* Load and execute JS via AJAX | |
* gist.github.com/englishextra/8dc9fe7b6ff8bdf5f9b483bf772b9e1c | |
* IE 5.5+, Firefox, Opera, Chrome, Safari XHR object | |
* gist.github.com/Xeoncross/7663273 | |
* modified callback(x.responseText,x); to callback(eval(x.responseText),x); | |
* stackoverflow.com/questions/3728798/running-javascript-downloaded-with-xmlhttprequest | |
* @param {String} u path string | |
* @param {Object} [f] callback function | |
* @param {Object} [e] on error callback function | |
* ajaxLoadTriggerJS(u,f,e) | |
*/ | |
;(function () { | |
var ajaxLoadTriggerJS = function (u, f, e) { | |
var w = window, | |
x = w.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); | |
x.overrideMimeType("application/javascript;charset=utf-8"); | |
x.open("GET", u, !0); | |
x.withCredentials = !1; | |
x.onreadystatechange = function () { | |
if (x.status == "404") { | |
if (e && "function" === typeof e) { | |
e(); | |
} | |
console.log("Error XMLHttpRequest-ing file", x.status); | |
return !1; | |
} else if (x.readyState == 4 && x.status == 200 && x.responseText) { | |
try { | |
var Fn = Function; | |
new Fn("" + x.responseText).call("undefined" === typeof window ? "undefined" === typeof self ? "undefined" === typeof global ? this : global : self : window); | |
} catch (m) { | |
throw new Error("Error evaluating file. " + m); | |
} | |
if (f && "function" === typeof f) { | |
f(x.responseText); | |
} | |
} | |
}; | |
x.send(null); | |
}; | |
("undefined" !== typeof window ? window : this).ajaxLoadTriggerJS = ajaxLoadTriggerJS; | |
} | |
()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment