The onerror Event Handler
It is my opinion that a user would much rather not see an error, that's why I like this simple little script so much.
JavaScript 1.1 allowed a new event handler named "onerror". I've also seen it written "onError" and OnERROR. All three worked on my machines.
This Event Handler allows you to have something happen when the page throws an error. The way I've seen it used online is to tell the user what line the error came from. I guess that would be good for the developer, but I'm more concerned about the users. I have taken the onerror Event Handler and put it into a script that will redirect the user to a different page as soon as an error is thrown.
Now, it's up to you how you'd like to use this. I would suggest having a non-JavaScript form of the page. That means a little more work, but it will help to keep a JavaScript heavy site error free.
Without any more talking...here's the script:
<SCRIPT LANGUAGE="JavaScript1.1">
<!--
onerror = redirect;
function redirect()
{
parent.location='errorfreepage.html'
}
-->
</SCRIPT>
There's not much to it, eh? I do want to point out a few things though.
First, the SCRIPT tag does specifically denote JavaScript 1.1. The onerror Event Handler is set up to run a function titled "redirect". When an error occurs, the function triggers and the page is changed through the "parent.location" hierarchy line.
Easy. Here's the script in action. Click on the link below. It will take you to a page titled, "jserrorpage.html". There is a JavaScript error on that page. When the error hits, the page should change to something I titled "errorfreepage"
Give it a shot.
That's That
Put the script high on your page so that it is in RAM before any JavaScript errors can occur. That's the easy part. What you need to do now is figure out where you will send the people who arrive at a page that throws an error.
Enjoy!






Loading Comments...