Smart questions
Smart answers
Smart people
Join Tek-Tips Forums
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

LaneW (Programmer) (OP)
23 Mar 02 14:22
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 "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.

Thanks,
Lane
gerrygerry (TechnicalUser)
23 Mar 02 15:06
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 http://quickfreewebsite.com!

GIGN (Programmer)
24 Mar 02 18:51
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.

http://www.devguru.com

<bb/>

RedLion (Programmer)
25 Mar 02 10:14
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
gerrygerry (TechnicalUser)
25 Mar 02 18:57
Good tip Charl! I recommend using PHP to check the URL - using a method similar to Charl's would be VERY simple.

-gerrygerry
 Go To http://quickfreewebsite.com!

Guest (visitor)
26 Mar 02 5:03
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.
gerrygerry (TechnicalUser)
26 Mar 02 15:38
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.

-gerrygerry
 Go To http://quickfreewebsite.com!

mcgiver (IS/IT--Management)
27 Mar 02 5:04
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!');        
}    

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!

Back To Forum

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