Skip to content

Instantly share code, notes, and snippets.

@b1
Created January 26, 2013 01:40
Show Gist options
  • Save b1/4639537 to your computer and use it in GitHub Desktop.
Save b1/4639537 to your computer and use it in GitHub Desktop.
//You should use the [XHR Object][1], aka the "AJAX" method.
//[Here's an overview][2] as to how to start using it.
//Here's an excerpt:
// [1]: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest
// [2]: https://developer.mozilla.org/en/docs/AJAX
function makeRequest(url, callback) {
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
}
//i'd omit this if I don't support older browsers
else if (window.ActiveXObject) { // IE
try {
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!httpRequest) {
alert('Giving up :( Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState === 4 && httpRequest.status === 200) {
callback(httpRequest.responseText);
} else {
alert('There was a problem with the request.');
}
}
httpRequest.open('GET', url);
httpRequest.send();
}
//using
makeRequest('path/to/your/file', function (contents) {
myList.push(contents);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment