The onerror Event Handler

By Joe Burns


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

If you've ever been surfing and run into Web pages that threw JavaScript errors, you know what a pain they can be. If you use a lot of JavaScript on your pages, you know that errors are just part of the game. No matter how careful you are, sometimes your pages throw errors.

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.

Because of the nature of the command, the user may never even know that an error has occurred. There won't be an error box. There won't be the nasty little yellow triangle in the status bar. There won't be anything...except a new page.

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()



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.

Go to jserrorpage.html

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.


  • Web Development Newsletter Signup

    Invalid email
    You have successfuly registered to our newsletter.
Thanks for your registration, follow us on our social networks to keep up-to-date