trinaryatom Posted August 2, 2011 Share Posted August 2, 2011 Looking for some JavaScript that can tell if a file or website is available.For example: if (URL exists) { document.write(<div> + "Content Box" + </div>) }else { document.write(<div> + "Unavailable" + </div>) } A real time example would be something like say an embed code from a website and having an alternate setup for when the website was down. Link to comment Share on other sites More sharing options...
justsomeguy Posted August 2, 2011 Share Posted August 2, 2011 If you need to use Javascript, about the only thing you can do is send an ajax request and check the response status. There might be a delay there, the delay might be up to 30 seconds if the server exists but times out. The major downside to that is that the entire file will end up being downloaded, and that will only work for files on your own server. If you need to test remote files then Javascript alone is not an option. Link to comment Share on other sites More sharing options...
trinaryatom Posted August 2, 2011 Author Share Posted August 2, 2011 If you need to use Javascript, about the only thing you can do is send an ajax request and check the response status. There might be a delay there, the delay might be up to 30 seconds if the server exists but times out. The major downside to that is that the entire file will end up being downloaded, and that will only work for files on your own server. If you need to test remote files then Javascript alone is not an option.Ah, well... dang.So if i may ask, what is the proper code to do a something like this. PHP? Link to comment Share on other sites More sharing options...
justsomeguy Posted August 2, 2011 Share Posted August 2, 2011 With PHP you can at least send a request for only the headers to see if the content exists without downloading the whole thing. There's still a delay during the connection though. You can always have Javascript send an ajax request to a PHP script on your server which does the check and returns the result back to Javascript. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.