JavaScript : Mailing 404 Error Information

404-Error-343x230

When a someone tries 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.

Click here for an example of a broken link to see the 404-error served up by JSResource.com.

The 404 Error page displays the link that is 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>// <![CDATA[

loc = document.location.href;
ref = document.referrer;

document.write(“
<B>Broken Link:</B> “+loc+”

”);
document.write(“
<B>Referring Document:</B> “+ref+”

”);
// ]]></script>
This would display:

Broken Link: http://JSResource.com/javascript/js404.html
Referring Document: http://JSResource.com/javascript/validform.html
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>// <![CDATA[

document.write<A HREF=’mailto:web@JSResource.com?subject=404 URL: ” + loc +” REFERRER: ” + ref +”‘>web@JSResource.com</A>”);
// ]]></script>



Copyright 2017 JSResource.com. All rights reserved.

Posted January 3, 2017 by in category JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *