Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirect

Status
Not open for further replies.

lintow

Programmer
Nov 29, 2004
82
0
0
US
I have a link that goes to another webserver or page.

I want to create a redirect if I cannot get to that server from my link.
If there is not connection to the page (server) then display my custome page. Or redirect it to my custome page if the page cannot be displayed

can this be done?
any sample code?

any help on this would be greatly appreciated

 
You would of course need to test the validity of the first link to see if the page is available. I think the best method would be to perform an XMLHTTP request to test the validity of that page.

There have been a number of posts here covering this. Do a quick search to see what you can come up with.
I will look and see if I have saved the sample code others have posted. Or perhaps someone else will chip in code for it while I am looking. :)


Paranoid? ME?? WHO WANTS TO KNOW????
 
OK, here is some sample code.
Code:
<HTML>
<HEAD>
<TITLE></TITLE>
<script language="javascript">
var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}

function testURL(url) {
  xmlhttp.open("HEAD", url,true);
  xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
    if (xmlhttp.status==200) alert("URL Exists!")
      else if (xmlhttp.status==404) alert("URL doesn't exist!")
      else alert("Unexpected Status was returned: "+xmlhttp.status)
    }
  }
  xmlhttp.send(null)
}
</script>
</HEAD>
<BODY>
URL to test: <input type="text" id="URLTest" name="URLTest">&nbsp;&nbsp;
<input type="button" value="Test URL" onclick="testURL(document.getElementById('URLTest').value); return false;">
</BODY>
</HTML>

This code was taken almost entirely from so that would be a good place to go for further detail.
This is best done server-side so you do not run into browser issues if they do not support the XMLHTTP request but this script will run client-side at least in IE.


Paranoid? ME?? WHO WANTS TO KNOW????
 
are there any other methods than this one?
 
lintow said:
are there any other methods than this one?

You must have missed theniteowl's statement:

theniteowl said:
This is best done server-side

This is best done server-side. You should ask in whatever forum is most appropriate for the server software you are using.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top