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>
|
What do you want your Web site to do?

What do you want your Web site to do? Ektron CMS400.NET lets you do everything you need to do on the Web and do everything you want to do.
Ektron CMS400.NET gives you all the features you would expect from an
enterprise CMS, but also high-demand functionality like real-time Web
site analytics, community building, blogging, personalization, and
online calendars. Ektron CMS400.NET bridges your Web site gaps.
Use Ektron CMS400.NET for:
- Content & Document Management - Author/edit content, manage navigation, menus, audit trails, workflow, approvals
- Personalzation & Portal Support - Allow visitors to create their own view of your web page
- 508 & W3C Compliance Checker - Ensure compliance with scheduled or on-demand compliance reports
- Memberships - Build online communties with blogs and forums, enable email web alerts, build polls and surveys
- Web Site Analytics - track visitors, report on most requested pages, see where they came from and where they are going
- HTML Form Engine - Create a two-way dialog with visitors using our wizard-like HTML form-building engine with validation and database support
- Multilingual Support - Integrated translation tools help you reach multiple audiences by speaking their language
- Online Calendars - Build and manage Web-based calendars and enable users to customize a view based on what’s important to them
See why there are 15,000+ Ektron integrations worldwide.
Click here to learn more, then request a live online demo or download a FREE TRIAL
 |
|
Get Your Diploma!
W3Schools' Online Certification Program is the perfect solution for busy
professionals who need to balance work, family, and career building.
The HTML Certificate is for developers who want to document their knowledge of
HTML, XHTML, and CSS.
The ASP Certificate is for developers who want to document their knowledge of
ASP, SQL, and ADO.
|
|