Skip to content

Instantly share code, notes, and snippets.

@AndrewStanton94
Last active August 29, 2015 14:11
Show Gist options
  • Save AndrewStanton94/a0b446262a091d7cb612 to your computer and use it in GitHub Desktop.
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…
<!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 &ndash; First AJAX</h1>
<div id=grabdiv>
AJAX uploads go here.
</div>
</body>
</html>
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);
}
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