tags:

views:

102

answers:

2

I'm currently developing in a closed environment a web based "slide show" of sorts, using Javascript to move to the next slide (specifically document.location, after showing the current page for a delay of X seconds). An issue I'm having is that if the next page is for some reason offline, the entire slideshow breaks (page unavailable error, etc).

Is there a way using javascript to check to see if the next page is online before redirecting to it?

edit: I'm controlling the environment (intranet) - so no chance of Javascript being switched off.

A: 

Request the next page via Ajax, and don't redirect to the next page if you don't get a successful response.

tdavies
A: 

Personally i would'nt do that check in javascript, people may have javascript disabled. I think HttpRequest Object would be good to retrieve the HTTP Response.

Check out http://www.w3schools.com/dom/dom%5Fhttp.asp

<script type="text/javascript"> 

    function fileExists(URL){

     var httpRequest = window.XMLHttpRequest ? new XMLHttpRequest() 
          : window.ActiveXObject 
          ? new ActiveXObject("Microsoft.XMLHTTP") 
          : null;        
         httpRequest.open("HEAD",URL,false); 
     httpRequest.send(null); 
     return (httpRequest.status == 404) ? false : true;
    } 

    function init(){

     if (fileExists('http://www.mysite.com/images/anything.jpg'))
      {
       alert('That file exists');
      }
     else {
       alert('That file does not exist');
      }  
    }

    navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false); 

</script>
Elijah Woods
Use feature detection instead of `navigator.appName` sniffing.
Eli Grey