test URL availability using Javascript

Posted on 2012-09-02
Last Modified: 2012-09-04
I am attempting a web page that will redirect the client to one of either two other URLs (on completely separate servers), depending upon the availability of one or the other. That is, I need to test if the first one is available (reachable) and, if not, redirect to the alternate

Having looked through other posts on EE, I came across a suggestion (see my example below). It attempts to reach an image file on the first server and, if successful, assumes that server is up and running.

The problem: If it is successful in reaching the image once, upon subsequent trials it always returns "success", even if I delete from the server the image file it is trying to reach. I thought it was because the image file was being cached by my browser, but after deleting all browsing history (especially the temporary internet file option) it still thinks it is successful every time.

How can I force a new download of the image file each time? Or, perhaps there is a better way to approach the basic problem (testing the availability of a server?)

Here's my code...

<script language="JavaScript">
 var ImageObject = new Image();
 ImageObject.src = "http://Site1/any.jpg"; // try to get image
   alert("Ping worked!");
   //location.href = 'http://Site1';
 } else {
   alert("Ping failed :(");
   //location.href = 'http://Site2';

Open in new window

Question by:futuremoose
    LVL 50

    Expert Comment

    by:Julian Hansen
    If the url's are on different domains then you will need to go through a server proxy as browser security will prevent cross domain calls.
    LVL 2

    Accepted Solution


    i will try to first to correct your approach and then give you a new workaround.

    first, the problem may be caused by caching, if the content is not available, it could return a success status and fetch it from cache.

    to prevent it from doing so, you should add some random data in the URL, so you have to change the following line:

    ImageObject.src = "http://Site1/any.jpg"; // try to get image

    Open in new window

    to the following

    ImageObject.src = "http://Site1/any.jpg"+Math.random(); ; // try to get image

    Open in new window

    using this methode, your browser if forced to get the image directly from server and not from cache.

    New approach:

    you could use AJAX request to first server and check the returned status is 200. if not;  you may consider server not reachable.

    this tutorial may help you :

    Good luck!

    Author Comment

    Thanks fourck... I'll look into both the random number fix and the AJAX alternative.

    Author Comment

    There is another very simple approach in the thread "Q_25147630" that uses HTML only... I have tried it and it seems to work every time, although I'm not sure why it wouldn't run into a cached file problem as well.

    Author Comment

    BTW, fourck, the AJAX-based solution worked great... thanks a lot.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    "That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved." Ralph Waldo Emerson Introduction: One of the wonderful things about the web is that it makes it s…
    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now