Skip to content

Instantly share code, notes, and snippets.

@Tiny-Giant
Created February 25, 2016 06:46
Show Gist options
  • Save Tiny-Giant/c4f4285f9f159fc816f9 to your computer and use it in GitHub Desktop.
Save Tiny-Giant/c4f4285f9f159fc816f9 to your computer and use it in GitHub Desktop.
// If we query the nodes from the DOM first, we save CPU cycles.
var nodes = {};
nodes.row = document.getElementById("row");
nodes.results = document.getElementById("results");
function getStates(str)
{
if (str.length == 0)
{
nodes.row.innerHTML = "";
nodes.results.innerHTML = "";
}
else
{
var xhr = new XMLHttpRequest();
// The load event will fire even if the request fails.
xhr.addEventListener('load', function()
{
// That way we can log the results to the console when it does fail.
if (xhr.status !== 200)
{
console.log(xhr.status, xhr.statusText, xhr.responseText);
return;
}
nodes.results.innerHTML = xhr.responseText;
}, false);
xhr.open('GET', 'support/getStates.php?q=' + str, true);
xhr.send();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment