Link to home
Create AccountLog in
Avatar of YourPCGP
YourPCGP

asked on

Check URL is reachable with Javascript

Hi All

I'm developing a website for a company with a link to their company intranet; this will only resolve with DNS from *inside* the network, though (fine, as outside users don't need access to the intranet)

However, I don't want users from outside just getting a generic 404 when they click the intranet link, and was wondering if there was a piece of javascript I could use to check that the URL is reachable from their machine; if not, pop-up a message box, if so, just follow the link.

Thanks in advance


George Preston
Avatar of joeposter649
joeposter649

You could mess arouund trying to use a hidden iframe or xmlhttp but you should probably do something on the server side instead.  Like don't dispay the link for a given IP range.  Or perhaps you could use something like LOGON_USER or REMOTE_USER to tell you if they're an internal user.  
BTW, they won't get a 404 error.  They'll get something like "Cannot find server or DNS error".
In IIS I know you can specify a custom designed page for each page error.
Look at the link provided directly below for more detail.

http://www.plinko.net/404/custom.asp

This is also useful for pages that have been removed or other page
errors so your visitors never get that ugly white error page.


Hope this helps!

:)

If they can't reach the intranet that means that they will never connect to the web server. This in turn means that you have no control over the error message that will appear on the client since it will simply time out.

If you really want to give custom error messages you would have to set up your web server that will offer those responses to be available from outside the intranet as well.

cheers,

Anastasiosyal
ASKER CERTIFIED SOLUTION
Avatar of lotsofish
lotsofish

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of YourPCGP

ASKER

Anastasiosyal thanks for your input but that is why I proposed javascript (client side) although I do not have much experience of it.

Regarding custom pages, these won't work because the pages are Asp.NET, so if a page retreival fails, it gives the .NET error (ugly looking thing) before any server error page.

JoePoster, can you elaborate on the Logon_user/Remote_user option, or give me some pointers as to how to implement your other suggestion (don't display link for given IP range) please?

Thanks

George
If you're using .net I would definatly do this on the server side.

Are you using any athentication?  If so you can probably use IsAuthenticated to determine how to set the visability on th anchor.  Or you could change the href to javacript:alert("not on internet").

Here's info on the server variables I mentioned...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/21b3be8f-d4ed-4059-8e21-6cba2c253006.asp
Note that REMOTE_ADDR is the ip address.  Depending on how your network is set up this might be the IP of a proxy and not the user's machine.  It might be easier if that's the case.
I got my idea to work.  I tested it and made a couple of small changes.  Here is the code.

<html><head>
<script language="javascript">

function checkImage() {
var i = new Image;
i.onerror=setpopup;
i.src="http://this.is.a.small.image.on.your.server.gif";

}

function setpopup(){
yourlink.href="javascript:window.alert('Error, you are not on the intranet');";
}


</script>

</head>

<body onload="checkImage();">
<a href="http://intranet/site/" name="yourlink">INTRANET</a>
</body>
</html>
Lotsofish, many thanks - exactly what I wanted and works perfectly. Sorry about low points, am a newbie.

You van see your handiwork here: http://81.138.70.68/ (it's in development)

George
Cool.  Glad to help.