please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 21
JavaScript Reference
Next
 

Window.onerror() Handler

Name

Window.onerror() Handler---invoked when a JavaScript error occurs

Availability

Navigator 3.0

Synopsis

You register an onerror() event handler like this:

window.onerror=handler-func 

Navigator invokes the handler like this:

window.onerror(message, url, line)

Arguments

message

A string that specifies the error message for the error that occurred.

url

A string that specifies the URL of the document in which the error occurred.

line

A number that specifies the line number at which the error occurred.

Returns

true if the handler has handled the error and JavaScript should take no further action. false if JavaScript should post the default error message dialog box for this error.

Description

The onerror() event handler of the Window object is invoked when a JavaScript error occurs in code executing in that window. The default error handler installed by JavaScript displays an error dialog box. You can customize error handling by providing your own onerror() event handler.

You define an onerror() event handler for a window by setting the onerror property of a Window object to an appropriate function. Note that unlike other event handlers in JavaScript, the onerror() handler cannot be defined in an HTML tag.

When the onerror() handler is invoked, it is passed three arguments. The first is a string specifying the error message. The second is a string specifying the URL of the document in which the error occurred. And the third is a number that specifies the line number at which the error occurred. An error handling function may do anything it wants with these arguments: it may display its own error dialog, or may log the error in some way, for example. When the error handling function is done, it should return true if it has completely handled the error and wants JavaScript to take no further action. Or, it should return false if it has merely noted or logged the error in some fashion and still wants JavaScript to display the error message in its default dialog box.

Note that while this event handler returns true to tell the browser to take no further action, most Form and form element event handlers return false to prevent the browser from performing some action, such as submitting a form. This inconsistency can be confusing.

You can turn off error handling entirely for a window by setting the onerror property of the window to null. If you will later want to turn error handling back on, you should first save the default error handler in a temporary variable, so you can restore the onerror property to its default value.

Example

The following code shows how you might write and register an error handler for a window. Instead of reporting the error in a dialog box, this handler reports the details in a form that it creates directly in the document itself. The form contains a button that will send the details of the error off to the author of the web page.

<script>
function p(s) { document.writeln(s) }  // shorthand
// define the error handler. It generates an HTML form so
// the user can report the error to the author.
function report_error(msg, url, line)
{
   // Output a form that reports the error
   p('<P><HR SIZE=5><DIV align=center>');
   p('<H1>SORRY!  A JavaScript Error Has Occurred</H1>');
   p('<FORM ACTION="mailto:bugs at wahoo.com" METHOD=post
   p('      ENCTYPE="text/plain">');
   p('You can help the author debug this program by clicking here: ');
   p('<INPUT TYPE="submit" VALUE="Report Error">');
   p('</DIV><DIV align=right>');
   p('<BR>Your name (optional): <INPUT SIZE=60 NAME="name" VALUE="index.html">');
   p('<BR>Message: <INPUT SIZE=60 NAME="message" VALUE="'
               + msg + '">');
   p('<BR>URL: <INPUT SIZE=60 NAME="url" VALUE="' + url + '">');
   p('<BR>Line Number: <INPUT SIZE=60 NAME="line" VALUE="'
               + line + '">');
   p('<BR>Browser Version: <INPUT SIZE=60 NAME="version"'
               + 'VALUE="' + navigator.userAgent + '">');
   p('</DIV>');
   p('</FORM>');
   p('<HR SIZE=5><P>');
   return true;    // tell the browser not to report the error itself
}
// now register the error handler
window.onerror =index.html report_error;
</script>

See Also

"Window"


Previous Home Next
Window.onblur() Book Index Window.onfocus()

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/JAVA/SCRIPT/definitive/refp_449.htm, 9KB, , updated: 2019/10/14 15:00, local time: 2024/12/23 08:49,
TOP NEW HELP FIND: 
18.189.143.150:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://sxlist.com/techref/language/JAVA/SCRIPT/definitive/refp_449.htm"> [Chapter 21] Reference: Window.onerror()</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to sxlist.com!


Site supported by
sales, advertizing,
& kind contributors
just like you!

Please don't rip/copy
(here's why

Copies of the site on CD
are available at minimal cost.
 

Welcome to sxlist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .