Created
October 28, 2013 07:25
-
-
Save detailyang/7192649 to your computer and use it in GitHub Desktop.
ajax
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
<span id="ajaxButton" style="cursor: pointer; text-decoration: underline"> | |
Make a request | |
</span> | |
<script type="text/javascript"> | |
(function() { | |
var httpRequest; | |
document.getElementById("ajaxButton").onclick = function() { makeRequest('test.html'); }; | |
function makeRequest(url) { | |
if (window.XMLHttpRequest) { // Mozilla, Safari, ... | |
httpRequest = new XMLHttpRequest(); | |
} 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 = alertContents; | |
httpRequest.open('GET', url); | |
httpRequest.send(); | |
} | |
function alertContents() { | |
alert("trigger"); | |
if (httpRequest.readyState === 4) { | |
if (httpRequest.status === 200) { | |
alert(httpRequest.responseText); | |
} else { | |
alert('There was a problem with the request.'); | |
} | |
} | |
} | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment