A window is the main Web page, unless it is divided up into frames. There are a number of events that will affect windows and frames ; these are described in Table 12.3. The following examples illustrate some of the events that affect windows and frames. Table 12.3. Window and frame events.
12.3.1 The onLoad and onUnLoad EventsThe onLoad event handler is invoked when a document, its frameset, or images have completely finished loading. This includes the point at which all functions have been defined and scripts have been executed, and all forms are available. This event can be helpful in synchronizing the loading of a set of frames, particularly when there may be large images that need to be loaded or all of the frame data hasn't arrived from the server. The onUnLoad event handler is invoked when the page is exited or reset. Example 12.5<html><head><title>Mouse Events</title> 1 <script language="JavaScript"> 2 var sec=0; 3 function now() { var newdate= new Date(); var hour=newdate.getHours(); var minutes=newdate.getMinutes(); var seconds=newdate.getSeconds(); var timestr=hour+":"+minutes+":"+seconds; 4 window.setInterval("trackTime()", 1000); 5 alert("Your document has finished loading\n"+ "The time: "+timestr); } 6 function trackTime(){ 7 sec++; } 8 function howLong(){ alert("You have been browsing here for "+ sec+" seconds"); } </script> </head> 9 <body background="blue hills.jpg" onLoad="now();" 10 onUnLoad="howLong();"> <font face="arial,helvetica" size=5> When you leave or reload this page, <br>an alert dialog box will appear. </body> </html> EXPLANATION
12.3.2 The onload() and ununload() MethodsThe onload() method, window.onload() , simulates the onLoad event handler. The ununload() method, window.ununload() , simulates the behavior of the onUnload event handler. All JavaScript event methods must be in lowercase, such as onload() or ununload() . The event handlers themselves , used as HTML attributes, are not case sensitive, so ONUNLOAD, onUnLoad , and onunload are all acceptable. 12.3.3 The onFocus and onBlur Event HandlersWhen an object has the focus, it is waiting for the user to do something, such as press a button, click on a link, start or stop an animation. If you are moving between frames, the frame where the mouse is pointing has the focus, and when the cursor moves out of the frame, it loses focus or is "blurred." The onFocus event handler allows you to initiate a window or frame type function when the mouse is moved into a window, and the onBlur event handler is triggered when you leave a window or frame. When a window has focus, it becomes the top window in a stack of windows. The following example changes the background color of the left frame to pink when it goes into focus and to yellow when it goes out of focus. The status bar at the bottom of the window reflects what frame has the focus. Example 12.6<html> <head><title>Frame Me!</title></head> 1 <frameset cols="25%,75%"> 2 <frame src="leftfocus.html" name="left"> 3 <frame src="rightfocus.html" name="right" > </frameset> </html> ---------------------------------------------------------------------- <!-- The right frame file --> <html> 4 <head><title>Right Frame</title></head> 5 <body bgColor="lightblue"> <font face="arial" size=4> right frame<br> </body> </html> ---------------------------------------------------------------------- <html> <head><title>Left Frame</title> 6 <script language="JavaScript"> 7 function focus_on_me() { document.bgColor="pink"; // Current doc is the left frame 8 window.status="focus leftframe"; } 9 function defocus_me() { parent.left.document.bgColor="yellow"; // Another way to // reference 10 window.status="focus rightframe"; // See the status bar } </script> </head> 11 <body onFocus="focus_on_me()" // Event handlers 12 onBlur="defocus_me()" bgColor="lightgreen"> <image src="signs.jpg"> </body> </html> EXPLANATION
12.3.4 The focus() and blur() MethodsThe focus() and blur() methods behave exactly the same as their like-named events. These methods are applied to an object, such as a window or form object, and are called from the JavaScript program. When the focus() method is applied to an object, it will cause that object to be in focus and when the blur() method is applied to an object, it will lose its input focus. Example 12.7<html> <head><title>The focus and blur methods</title> <script language="JavaScript"> 1 function newWindow(){ 2 winObj=window.open ("summertime.jpg", "summer","width=650,height=200,resizable=yes, scrollbars=yes,location=yes"); 3 winObj.moveTo(0,0); // Move to left-hand corner of screen 4 winObj.focus(); // New window gets the focus //windObj.blur(); } 5 function closeWindow(){ 6 winObj.close(); // Close the new window } </script> </head> <body bgColor="lightgreen"> <h2>Summer Scene from the Old Country</h2> <form> <input type=button value="Open Window" 7 onClick="javascript:newWindow();"> <input type=button value="Close Window" 8 onClick="javascript:closeWindow();"> </form> </body> </html> EXPLANATION
|