JavaScript The onerror Event
Using the onerror event is the old standard solution to catch errors in a web page.
Examples
The onerror event
(an example with an error)
How to use the onerror event to catch errors in a web page.
The onerror Event
We have just explained how to use the try...catch
statement to catch errors in a web page. Now we are going to explain how to use
the onerror event for the same purpose.
The onerror event is fired whenever there is a script error in the page.
To use the onerror event, you must create a function to handle the errors.
Then you call the function with the onerror event handler. The event handler is called with three arguments: msg (error message), url (the url of the page that caused the error) and line (the line
where the error occurred).
Syntax
onerror=handleErr
function handleErr(msg,url,l)
{
//Handle the error here
return true or false
}
|
The value returned by onerror determines whether the browser displays a
standard error message. If you return false, the browser displays the standard
error message in the JavaScript console. If you return true, the browser does not display
the
standard error message.
Example
The following example shows how to catch the error with the onerror event:
<html>
<head>
<script type="text/javascript">
onerror=handleErr;
var txt="";
function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n";
txt+="Error: " + msg + "\n";
txt+="URL: " + url + "\n";
txt+="Line: " + l + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
return true;
}
function message()
{
adddlert("Welcome guest!");
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>
|
The Ektron Intranet
lets you do everything you need to do on your corporate intranet and everything you want to do... all with just one application.
What can you do with the Ektron Intranet? |

|
Navigate through content, documents, assets, colleagues and workgroups quickly and intuitively with enterprise search |

|
Communicate with friends and colleagues with forums, message boards and corporate blogging using the new Social Networking Platform |

|
Promote collaboration among coworkers in your organization through project workspaces where others can efficiently find information and work together |

|
Personalize your company profile by bookmarking and organizing favorite content, uploading assets, posting photos, blogging, and more |

|
Interact with features like tagging, flagging, wikis and ratings found in the Web 2.0 Toolbox |
 |
Author/edit content, manage navigation, menus, audit trails, workflow and approvals with the best in breed Content Management |
|
|
|
|
See why there are 20,000+ Ektron integrations worldwide. Request an
INSTANT DEMO or download a
FREE TRIAL today. |
|