Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
}
@vin-sha

This comment has been minimized.

vin-sha commented Jun 17, 2013

This doesn't work!

Always returns false

@jpsilvashy

This comment has been minimized.

Owner

jpsilvashy commented Nov 18, 2013

@vinaykrsharma, It's working great for me, are you trying this on your local server or on a remote server?

@aledrus

This comment has been minimized.

aledrus commented Mar 6, 2014

Hi,

I get this error:
Bad constructor. (W056) var xhr = new ( window.ActiveXObject || XMLHttpRequest )( "Microsoft.XMLHTTP" );

@Parthmy007

This comment has been minimized.

Parthmy007 commented Mar 15, 2014

how to check ? it's return always false

@fallen90

This comment has been minimized.

fallen90 commented May 13, 2014

It's working for me ^_^ convert this to a runnable script, then run this from the console in chrome developer tools.

// 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();
console.log ( xhr.status >= 200 && xhr.status < 300 || xhr.status === 304 );
} catch (error) {
console.log(error);
}

Working for localhost and remote (github)

@cmpscabral

This comment has been minimized.

cmpscabral commented Jun 11, 2014

thanks for this script :) I just added an option to check the port, if needed - https://gist.github.com/cmpscabral/7626ae29475283d32771

@juanvelez89

This comment has been minimized.

juanvelez89 commented Aug 28, 2014

but now local always return true.

@Hettomei

This comment has been minimized.

Hettomei commented Sep 29, 2014

Hi,

Thank you for this simple gist.

If you have a port problem, or if you code on a "localhost:8888", try to use window.location.host instead of window.location.hostname

@michaellopez

This comment has been minimized.

michaellopez commented Dec 23, 2014

@sumit63626

This comment has been minimized.

sumit63626 commented Feb 25, 2015

Hi i am trying this on my local server and using xhr.open( "HEAD","http://www.google.com" , false ); and it is always returning false. KIndly help

@BarbuE

This comment has been minimized.

BarbuE commented Jan 27, 2016

@sumit read about CORS

@davidwu220

This comment has been minimized.

davidwu220 commented Aug 16, 2016

// Open new request as a HEAD to the root hostname with a random param to bust the cache << Why do you have to bust the cache? And how is it going to bust the cache? Thanks!

@mhdSid

This comment has been minimized.

mhdSid commented Sep 14, 2017

And if you want to wait until online you can use this:

public waitUntilOnline(): any {

        return new window.Promise((resolve, reject) => {  

        	let interval: any = window.setInterval(() => {   

        		if (this.isOnline()) {   

        			window.clearInterval(interval);  
 
        			interval = undefined;   

        			resolve(true); 
  
        		}  else {   

        			console.log('Browser is still offine...');  
 
        		}   

        	}, 2500);   

        });   
}

this.waitUntilOnline().then((isOnline: boolean) => {
 //always returns true here. Do anything when the browser switches from offline to online
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment