11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

you can replace or suppress the error messages shown in Netscape 3 and Internet Explorer<br />

(with debugging turned on) and the output to the <strong>JavaScript</strong> Console in Netscape 4+. <strong>The</strong><br />

values to which window.onerror can be set and the effects of doing so are outlined in Table<br />

23-3.<br />

Table 23-3: window.onerror Values and Effects<br />

Value of<br />

window.onerror<br />

Effect<br />

Null Suppresses reporting of runtime errors in Netscape 3+.<br />

A function <strong>The</strong> function is executed whenever a runtime error occurs. If the<br />

function returns true, then the normal reporting of runtime errors is<br />

suppressed. If it returns false the error is reported in the browser<br />

as usual.<br />

Note <strong>The</strong> onerror handler is also available for objects other than Window in many browsers,<br />

most notably the and elements.<br />

For example, to suppress error messages in older browsers you might use<br />

function doNothing() { return true; }<br />

window.onerror = doNothing;<br />

window.noSuchProperty() // throw a runtime error<br />

Since modern browsers don‘t typically display script errors unless users specifically configure<br />

them to do so, the utility of the return value is limited.<br />

<strong>The</strong> truly useful feature of onerror handlers is that they are automatically passed three values<br />

by the browser. <strong>The</strong> first argument is a string containing an error message describing the error<br />

that occurred. <strong>The</strong> second is a string containing the URL of the page that generated the error,<br />

which might be different from the current page if, for example, the document has frames. <strong>The</strong><br />

third parameter is a numeric value indicating the line number at which the error occurred.<br />

Note Early versions of Netscape 6 did not pass these values to onerror handlers.<br />

You can use these parameters to create custom error messages, such as<br />

function reportError(message, url, lineNumber)<br />

{<br />

if (message && url && lineNumber)<br />

alert("An error occurred at "+ url + ", line " + lineNumber +<br />

"\n<strong>The</strong> error is: " + message);<br />

}<br />

return true;<br />

window.onerror = reportError; // assign error handler

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!