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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking if URL Exists

Status
Not open for further replies.

LaneW

Programmer
Mar 23, 2002
2
US
Hello everyone,

I am new here and this might have been addressed already.

I am looking for a way with JavaScript to check to see if a URL Exists and if not then redirect to another URL.

Example:
When someone clicks on a link or a button the javascript would check to see if a remote URL " " exists. If it does exist then send them to that page. If it does not exist then send them to a local URL "testnotfound.html".

I hope this makes sense. Any help would be greatly appreciated.

Thanks,
Lane
 
All you can do is validate that it is a valid url... there is no way to check to make sure it points to a valid file (with javascript anyway). You need a something written in Perl or PHP to do this (something server-side).

Good luck! -gerrygerry
Go To
 
Is there any real need to do this?

Is it part of an application where links are added all the time? Like by users loggin in with browsers?

Probabaly better to check using your server like gerry said, but potentially you could use an [IE5.5+] XMLHTTP connection.


<bb/>
 
Hello Lane,

Once I've written an application in Visual FoxPro that checked if a url existed. The primary goal was getting the result from a request to a url.

When the site didn't exist you would get a html page returned with a HTTP 404 error as title. So when you read the html source and search if <title>HTTP 404 Not found</title> exist within this source you will know if the site exists or not(or if it's in the eare or not).

I hope it will help,

Charl
 
Hello,

It seems to me that I mispelled my question (may be my bad english is responsible).

I don't want my goodbye() script (called by the unload event handler) to test if the URL invoked when leaving my site is valid or not, I just want to test if this URL is out of my site, ie. if it does not begin by the FQDN of my site. If the URL is out of my site, the script issues a popup window, if not, the script does nothing.

The purpose of this is to have a popup window asking the visitor to leave a comment on my site, but only when he gets out of it, and not when he chooses to leave a page of my site to go to another page of the same site.

I just miss one thing to write this algorithm : it's the name of the element which contain the invoked URL when the unload event handler is ran.

Thanks in advance.
 
That's easy with javascript! Change your body tag to something similar to this:
Code:
<body onUnLoad=&quot;if(window.location.indexOf('FQDN')==-1){goodbye();}&quot;>

This will check the new location string for &quot;FQDN&quot; anywhere in the string. If &quot;FQDN&quot; isn't in the string, goodbye() function will be called. -gerrygerry
Go To
 
Try this:

//
var loc = location.href.valueOf();
var comparison = false;
// Checks for something in the URL sring
// Could be a file name, domain, a variable etc.
if (loc.indexOf(&quot;my unique string&quot;)!=-1){
comparison = false;
}
else {
comparison = true;
}
if (comparison){
// In your case, this would be a redirect
document.write('You see me because something in the URL string DOES NOT match!');
}
else {
document.write('You see me because something in the URL string matches!');
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top