Last active
August 29, 2015 14:11
-
-
Save AndrewStanton94/a0b446262a091d7cb612 to your computer and use it in GitHub Desktop.
Ugly way of getting a resurce from server.
NOTE, this requires a server, a file:/// (same origin) URL for the get will fail on line 15.js
This is bad because 1) using .event to add function 2) Need to check status and readystate. 4 calls for each fetching of a resource. Changes.
V2: Putting code into a function.
V3: Breaking function into 2. 1 f…
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<title>ds3a</title> | |
<meta charset = 'UTF-8'> | |
<link rel='icon' href='favicon.ico'> | |
<script src='ds3d.js'></script> | |
</head> | |
<body> | |
<h1>Dynasite 3a – First AJAX</h1> | |
<div id=grabdiv> | |
AJAX uploads go here. | |
</div> | |
</body> | |
</html> |
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
function AjaxGet(URL, callback) | |
{ | |
var ajaxObj = new XMLHttpRequest(); | |
ajaxObj.open("GET", URL, true); // The True implies asynchronous | |
ajaxObj.onreadystatechange = function() | |
{ | |
if (ajaxObj.status == 200) | |
if (ajaxObj.readyState == 4) | |
callback(ajaxObj.responseText); | |
}; | |
ajaxObj.send(null); | |
} |
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
function grabber (response) { | |
document.getElementById('grabdiv').innerHTML = response; | |
console.log('Grabbed by ds3d'); | |
} | |
AjaxGet('datafile.txt', grabber); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment