Detecting if a site is online.

Chris252

Hello everyone, I was wondering if it is possible to detect if a site is online using JavaScript. For example if google.com is online then alert("Online"); and if google.com is offline then alert("Offline");

I want to use this for two things, one is to detect if a particular URL is online and the other is to detect if the user has an internet connection by detecting if the user can access a site that very rarely goes offline.

Basically, I want to check if the user is able to access a website like google.com and if not alert("Offline"); and if so alert("Online");

Any help would be very much appreciated.

Chris

P.S: I would not like to use XMLHttp for this.

Angry_Black_Man

i cant think of any way offhand, but someone on a random website suggested using an image to determine the status of a site:

Be aware that this method relies on the site continuing to have the image available in the same location. It may be more reliable to download the favicon.ico file or whatever name it may have.

Gray1989

I had an idea to do this one time but never got around to it... My idea was for with a website I owned but it should work just aswell for any other site, and it doesn't require the client having to download an image file, but rather a much smaller javascript file.

Since you can embed .js files in your page across webservers, the idea was to use something like this:

<script src="http://server1.com/jsfile.js"></script>

The jsfile.js would contain variable declarations, ex:

var VariableX = "some variable input";

So, you can use code like the following in order to get what you want with JavaScript only...

Logic_Ali

Gray1989

No, not necessarily. All you need is to know is what is declared in the .js file...

Gray1989

Then again there may be some sort of AJAX error code you could use to check the server's URL directly, would be the best way....

trionix

Gray1989;1213053 wrote:

Then again there may be some sort of AJAX error code you could use to check the server's URL directly, would be the best way....

Thanks for your answer!

But with a simple GET request on a url like google.com, I encounter the same problem in Safari: he always says "online" even you are not.

Everything I tried, GET url requests, image loads, whatever... It always works in any browser except Safari...

Do you know perhaps a special script just working on safari?

Thanks you!

Gray1989

I just realized you're wanting the page to check if the user is online by checking a site for availability... So does that mean you're running the page locally (not on an online webserver)? If so, then you actually do have extra privileges which include being able to access a third-party site in an iframe with JavaScript. Using that method you should be able to accomplish your task...

Gray1989

In order to access an iframe with javascript, you may use something similar to the following code:

<iframe src="http://www.google.ca/" ID="googleframe"></iframe>

Once everything is loaded fine, you could try the 'onload' attribute on the body tag or set some sort of timeout with window.setTimeout() to start the following code:

Mind you I haven't tested this so it may require modification. Also it will not work if uploaded to a webserver as browsers disable the ability for webservers to run client-side code against each other...

toicontien

The reason GET requests are not reliable is that browsers can cache the response from the server. HTML5 has some features you might find useful. Try this blog post to see if it points you in the right direction:

toicontien

trionix

toicontien;1213456 wrote:

The reason GET requests are not reliable is that browsers can cache the response from the server. HTML5 has some features you might find useful. Try this blog post to see if it points you in the right direction:

If that doesn't get you what you need, try searching Google for html5 detect site offline.

Thank you very much toicontien, you showed me some really nice articles about this topic and from there I found much more interesting posts and finally my solution, that especially WORKS on SAFARI and IPAD!

This works great, and actual I just have to divide the alert in two different <div> messages on my body....have some problems here right now, because Safari seems not to like every kind of pushing out he message/state...I'll keep you posted and feel free to share your ideas on developing the error message output further on!

Thank you all!

Logic_Ali

It's the random() part that's doing it, which is equivalent to what I gave you in the first place.

Gray1989

<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">

I think you may be able to use this meta tag in order to prevent caching of the file, but you'd have to make it an html file. You can try using an iframe instead of an image but you should be able to use it in the same way. At least you won't have to use a random number with a "GET" request. Just a thought....