Entry
How do I check the HTTP response status of a link's URL?
How can I check the HTTP response status of all links in a document?
Feb 17th, 2001 06:56
Martin Honnen,
With NN4 you can use Java via liveconnect to open a socket connection
to a HTTP server, send an HTTP HEAD request and read the response
status line. This works only if the http server is up and is the same
as that your script was loaded from or you successfully request
privilege.
With IE5 there is the Active X Object
Microsoft.XMLHTTP
which allows to send HTTP requests and check the response status.
Depending on the security settings this object can connect to all
servers (usually when the script is loaded locally) or only to the
server the script comes from.
The following page contains code for NN4 and IE5 to read the HTTP
status of a URL and shows an example application where the status of
all links in the document is read. Note that this only works if the
given HTTP server exists (but maybe the url doesn't exist on it) while
the code can't check whether the server exists or is running.
<HTML>
<HEAD>
<TITLE>
link status check
</TITLE>
<SCRIPT>
function fetchHTTPStatus (url) {
if ((location.host == '') || (url.indexOf(location.host) == -1))
netscape.security.PrivilegeManager.enablePrivilege
('UniversalConnect');
var urlObj = new java.net.URL (url);
var host = urlObj.getHost();
var port = (urlObj.getPort() > 0) ? urlObj.getPort() : 80;
var fileName = urlObj.getFile();
var sock = new java.net.Socket (host, port);
dock = new java.io.DataOutputStream(sock.getOutputStream());
dock.writeBytes('HEAD ' + fileName + ' HTTP/1.0\r\n');
dock.writeBytes('\r\n');
var dis = new java.io.DataInputStream(sock.getInputStream());
line = dis.readLine(); // get just status message
dis.close();
dock.close();
sock.close();
var statusObject = new StatusObject(line);
return statusObject;
}
function StatusObject (statusLine) {
var re = /(HTTP\/[\d\.]+) (\d\d\d) ?(.*)$/i;
var match = re.exec(statusLine);
this.protocol = match[1];
this.status = match[2];
this.message = match[3];
}
function checkURLStatus (url) {
if (document.getElementById && window.ActiveXObject) {
var http = new ActiveXObject('Microsoft.XMLHTTP');
http.open('HEAD', url, false);
http.send();
return http.status;
}
else if (document.layers && navigator.javaEnabled()) {
return fetchHTTPStatus(url).status
}
else
return null;
}
function checkLinks () {
for (var l = 0; l < document.links.length; l++) {
window.status = 'checking link ' + l;
var link = document.links[l];
var status = checkURLStatus(link.href);
if (status) {
if (document.all) {
if (status == 200)
link.insertAdjacentHTML('afterEnd',
'<SUP><SPAN STYLE="color: green;">' + status + '<\/SPAN></SUP>');
else
link.insertAdjacentHTML('afterEnd',
'<SUP><SPAN STYLE="color: red;">' + status + '<\/SPAN></SUP>');
}
else if (document.layers) {
link.ol = new Layer(20);
link.ol.document.open();
if (status == 200)
link.ol.document.write('<FONT COLOR="green">' + status
+ '<\/FONT>');
else
link.ol.document.write('<FONT COLOR="red">' + status
+ '<\/FONT>');
link.ol.document.close();
link.onmouseover = showStatus;
link.onmouseout = hideStatus;
}
}
}
window.status = '';
}
function showStatus (evt) {
evt.target.ol.left = evt.pageX;
evt.target.ol.top = evt.pageY;
evt.target.ol.visibility = 'show';
}
function hideStatus (evt) {
evt.target.ol.visibility = 'hide';
}
</SCRIPT>
</HEAD>
<BODY ONLOAD="checkLinks();">
<A HREF="http://www.kibo.com">
Kibo
</A>
|
<A HREF="http://www.kibo.com/god">
god
</A>
|
<A HREF="http://www.faqts.com/js">
js
</A>
|
<A HREF="http://www.faqts.com">
faqts
</A>
</BODY>
</HTML>