If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Detecting if a site is online.

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");

1. If you reply to my post, and your reply would then appear directly beneath my post, DON'T QUOTE MY ENTIRE POST!!! IT'S REDUNTANT!!! IT'S ASININE!!!! IT'S REDUNDANTLY ASININE!!!!! DON'T DO IT!!!!
2. jQuery extends the functionality of JavaScript. If you don't know JavaScript, give up on that jQuery script and learn JavaScript. You'll save yourself a lot of frustration, I promise.
3. Use the [code][/code] tags. Otherwise, you may be left wondering why no one responded to your eyesore of a thread.

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:

HTML Code:

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

The jsfile.js would contain variable declarations, ex:

Code:

var VariableX = "some variable input";

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

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...

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...

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:

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!