mkaz.com home photography web dev personal about

JavaScript : Mailing 404 Error Info

Last Updated: 1999-01-30

When a someone trys to go to a page that is not on this site they will get a 404 error. A 404 error is the HTTP server code for file not found. The server then displays an error message which is just a HTML message that can be specified, this means it can be customized and made useful.

The 404 Error page displays the link that is broken and the referring document on the page. There is also a link to an e-mail address on the page which will put this information into the subject line of the message.

Gathering the Info

JavaScript is used to gather the information. Two document properties are used the 'location' and the 'referrer', cgi programmers note the proper spelling. The location is the URL of the current page you are on. This will show as the broken link because that is the page you are trying to request from the server. The referrer is the URL of the page you just came from.

An example:

<SCRIPT LANGUAGE="JavaScript">
loc = document.location.href;
ref = document.referrer;

document.write("<P><B>Broken Link:</B> "+loc+"</P>");
document.write("<P><B>Referring Document:</B> "+ref+"</P>");
</SCRIPT>

This would display:


Mailing out the Information

The best way to do this would be to have a sever-side script automatically mail out the information above on each 404 error. I don't want an e-mail for every broken link on my site, which may or may not be legitimately a broken link. So if I require a user to send out the link I will get fewer e-mails, and the e-mails should be more accuratly representing a broken link.

So I use a simple mailto: to send out the link, and have the subject line already specified in the e-mail message. This is done with by adding ?subject=SUBJECT LINE to the end of the mailto.

The above document information is written out to the mailto statement using JavaScript document.write function and including the above variables.

Example Code:

<SCRIPT LANGUAGE="JavaScript">
document.write<A HREF='mailto:user@host.com?subject=404 URL: " + loc +" REFERRER: " + ref +"'>user@host.com</A>");
</SCRIPT>


Related Links: