15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

140<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

Example<br />

If you want to turn off buffering for an entire page, include the following statement<br />

at the beginning of your script statements:<br />

window.offscreenBuffering = false<br />

onerror<br />

windowObject.onerror<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓ ✓ ✓ ✓<br />

Example<br />

In Listing 16-10, one button triggers a script that contains an error. I’ve added an<br />

error-handling function to process the error so that it opens a separate window and<br />

fills in a textarea form element (see Figure 2-2). If you load Listing 16-10 in NN6,<br />

some of the reporting categories report “undefined” because the browser unfortunately<br />

does not pass error properties to the handleError() function. A Submit<br />

button is also provided to mail the bug information to a support center e-mail<br />

address — an example of how to handle the occurrence of a bug in your scripts.<br />

Listing 16-10: Controlling Script Errors<br />

<br />

Error Dialog Control<br />

<br />

// function with invalid variable value<br />

function goWrong() {<br />

var x = fred<br />

}<br />

// turn off error dialogs<br />

function errOff() {<br />

window.onerror = doNothing<br />

}<br />

// turn on error dialogs with hard reload<br />

function errOn() {<br />

window.onerror = handleError<br />

}<br />

// assign default error handler<br />

window.onerror = handleError<br />

// error handler when errors are turned off...prevents error dialog<br />

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

function handleError(msg, URL, lineNum) {<br />

var errWind = window.open(“”,”errors”,”HEIGHT=270,WIDTH=400”)<br />

var wintxt = “”

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

Saved successfully!

Ooh no, something went wrong!