Skip to content

Instantly share code, notes, and snippets.

@duckinator
Forked from ldunn/gist:266612
Created December 31, 2009 04:42
Show Gist options
  • Save duckinator/266616 to your computer and use it in GitHub Desktop.
Save duckinator/266616 to your computer and use it in GitHub Desktop.
function loadDoc(url, callback) {
// branch for native XMLHttpRequest object
try {
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
req.onreadystatechange = (function(){ processReqChange(req, callback); })
req.open("GET", url, true);
req.send(null);
// branch for IE/Windows ActiveX version
} else if (window.ActiveXObject) {
isIE = true;
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = (function(){ processReqChange(req, callback); })
req.open("GET", url, true);
req.send();
}
}
} catch (e) {
window[callback](e.message);
}
}
// handle onreadystatechange event of req object
function processReqChange(req, callback) {
// only if req shows "loaded"
if ( req.readyState == 4 ) {
if ( req.statusText == "OK" ) {
window[callback](req.responseText);
} else {
window[callback]("Error fetching data: " + req.statusText);
}
}
}
function upvote() {
//loadDoc(url_as_str, callback_as_str)
//loadDoc('./up', 'foo();');
alert('YOU RANG?');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment