Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Checking if URL Exists

Checking if URL Exists

Checking if URL Exists

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.

When someone clicks on a link or a button the javascript would check to see if a remote URL "http://www.somedomain.com/test.html " 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.


RE: Checking if URL Exists

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!

 Go To http://quickfreewebsite.com!

RE: Checking if URL Exists

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.



RE: Checking if URL Exists

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,


RE: Checking if URL Exists

Good tip Charl! I recommend using PHP to check the URL - using a method similar to Charl's would be VERY simple.

 Go To http://quickfreewebsite.com!

RE: Checking if URL Exists


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.

RE: Checking if URL Exists

That's easy with javascript! Change your body tag to something similar to this:

<body onUnLoad="if(window.location.indexOf('FQDN')==-1){goodbye();}">

This will check the new location string for "FQDN" anywhere in the string. If "FQDN" isn't in the string, goodbye() function will be called.

 Go To http://quickfreewebsite.com!

RE: Checking if URL Exists

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("my unique string")!=-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!');        

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close