Last active
August 21, 2016 16:38
-
-
Save englishextra/12b7d8be1bc1016c0b252e5701d69016 to your computer and use it in GitHub Desktop.
JSON with JS.md
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
/*! | |
* 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 | |
*/ | |
var AJAXLoadAndTriggerJs = function (url, cb, data) { | |
var w = window; | |
try { | |
var x = w.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); | |
x.open(data ? "POST" : "GET", url, 1); | |
x.setRequestHeader("X-Requested-With", "XMLHttpRequest"); | |
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | |
x.onreadystatechange = function () { | |
if (x.readyState > 3) { | |
if (x.responseText) { | |
eval(x.responseText); | |
cb && "function" === typeof cb && cb(x.responseText); | |
} | |
} | |
}; | |
x.send(data); | |
} catch (e) { | |
console.log("Error XMLHttpRequest-ing file", e); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment