12.8 Handling Error Events


12.8.1 The onError Event

The error event fires when a JavaScript error has occurred (window) or when an image cannot be found (image elements).

Example 12.21
 <html><head><title>Wake up call</title>     <script language="javascript">         function wakeupCall(){    //  Function is defined here  timeout=setTimeout('alert("Time to get up!")',2000);         }      </script>     </head>     <body bgcolor="white">     <form>     <center>     <p> 1  <image src="Image/java_steam.gif"  2  onError="alert('Image is having trouble loading!')">  <p>     <input type="button"         value="Wake me"         onClick="wakeupCall()">     </form>     <center>     </body>     </html> 

EXPLANATION

  1. The <image> tag identifies the src of a .gif image to be loaded from a subdirectory called Image .

  2. The onError event handler is triggered when an error occurs while loading the image. See Figure 12.29.

    Figure 12.29. The onError Event handler was triggered because the image src was wrong (left), and after the image loads (right).

    graphics/12fig29.jpg



JavaScript by Example
JavaScript by Example (2nd Edition)
ISBN: 0137054890
EAN: 2147483647
Year: 2003
Pages: 150
Authors: Ellie Quigley

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net