?
Solved

test URL availability using Javascript

Posted on 2012-09-02
5
Medium Priority
?
1,795 Views
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
 if(ImageObject.height>0){
   alert("Ping worked!");
   //location.href = 'http://Site1';
 } else {
   alert("Ping failed :(");
   //location.href = 'http://Site2';
 }
</script>

Open in new window

0
Comment
Question by:futuremoose
  • 3
5 Comments
 
LVL 60

Expert Comment

by:Julian Hansen
ID: 38360219
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.
0
 
LVL 2

Accepted Solution

by:
fourck earned 2000 total points
ID: 38360225
Hi,

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 :
http://x10hosting.com/forums/tutorials/76824-checking-if-file-url-exists-using-javascript-ajax.html

Good luck!
0
 

Author Comment

by:futuremoose
ID: 38366078
Thanks fourck... I'll look into both the random number fix and the AJAX alternative.
0
 

Author Comment

by:futuremoose
ID: 38366145
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.
0
 

Author Comment

by:futuremoose
ID: 38366314
BTW, fourck, the AJAX-based solution worked great... thanks a lot.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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…
Suggested Courses

864 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