Skip to content

Instantly share code, notes, and snippets.

@chrismauck
Forked from jpsilvashy/hostReachable.js
Created March 12, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismauck/0a8c0299f61822e2fba2 to your computer and use it in GitHub Desktop.
Save chrismauck/0a8c0299f61822e2fba2 to your computer and use it in GitHub Desktop.
function hostReachable() {
// Handle IE and more capable browsers
var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );
var status;
// Open new request as a HEAD to the root hostname with a random param to bust the cache
xhr.open( "HEAD", "//" + window.location.hostname + "/?rand=" + Math.floor((1 + Math.random()) * 0x10000), false );
// Issue request and handle response
try {
xhr.send();
return ( xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 );
} catch (error) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment