Re: Check deadlink

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Martin Honnen (mahotrash_at_yahoo.de)
Date: 01/23/05


Date: Sun, 23 Jan 2005 16:01:09 +0100


Theewara Vorakosit wrote:

> How can I check whether a url is deadlink using Jscript/Javascript in IE
> before load it?

With IE/Win (5 and later) you can use the XMLHTTP request object and for
instance make a HEAD request and check the response status e.g.

function getHTTPStatus (url) {
   var httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
   httpRequest.open('HEAD', url, false);
   httpRequest.send(null);
   return httpRequest.status;
}

var httpStatus1 = getHTTPStatus('whatever.html');

If the page is not found the status code 404 is sent e.g. you could then
check
   if (httpStatus1 == 404) {
     // handle missing page
   }

But HTTP knows a lot of status codes and you will need to decide what
you regard as missing and not, the codes are documented here:
   <http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10>

And in general a script in a page loaded from http://example.com/ is
only allowed to connect back to that server so you can check
http://example.com/whatever.html and other URLs on that server but not
http://example.org/ or other hosts unless you change the IE settings.

-- 
	Martin Honnen
	http://JavaScript.FAQTs.com/