spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / diaries / 9

[next]

Web Project Manager
Aquent
US-PA-Collegeville

Justtechjobs.com Post A Job | Post A Resume
Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

The JavaScript Diaries: Part 9

  1. Introduction
  2. Data Types & Variables
  3. Operators
  4. Functions
  5. Conditional Statements and Loops
  6. Objects
  7. Browser-Based Objects
  8. Window Methods
  9. Window Event Handlers
  10. Navigator, Screen, History and Location Objects
  11. Arrays: Part 1 - Introduction
  12. Arrays: Part 2 - Multiple Array Types
  13. Arrays: Part 3 - Array Properties and Methods
  14. The Math Object
  15. The Date Object

By 

In our last two sections we looked at properties and methods of the window object. In this installment, we'll wrap up our study of the window object by learning how to use the most common of the window event handlers. We'll also take a look at some links for additional help.

Window Object Event Handlers

An event handler is a JavaScript keyword that allows a script to perform certain functions based on events on a Web page. Anytime something happens — a page loads, a link is clicked, the mouse moves — an event happens. Many times it's desirable to control these events. This can be done by using event handlers, which permit a script to interact with the visitor. A Web page can be more personable when events are performed based on actions taken by the individual visitor. This makes the page much more interesting than a static HTML document.

Think of the event handler as it's written. Using the onLoad event handler, you could say, "On loading the Web page, run the following JavaScript function when it's completed."

(Note: The capitalization of the actual event handler is optional, except when using XHTML. It can generally be written as "onload" or "onLoad." For our purposes, we'll use the latter format.)

Event handlers are usually placed in a form element, the opening body element, or in a link element. Occasionally they're used within the script itself. Let's take a look at the ones commonly used with the window object.

onLoad

This event handler is used to execute a script after a page has fully loaded. That includes loading all scripts, graphics, plugins and Java applets. This is different than what we've done up to now. When we called an alert window from a script located in the head section while the document was loading ("parsing"), the document stopped loading and ran the script, which then loaded the alert window. Once the alert window was closed, the document continued to load. Often JavaScripts are executed as the page is parsed. Beginning at the top of the Web page, scripts will be executed as they are encountered by the browser. Using the onLoad event handler, the entire page and all of its related files and components are loaded before the function listed in the onLoad event handler is executed.

This particular event handler is generally used within the body or frameset tag. An example would be:

<body onLoad="alert('This page has finished loading!)"

(While this will work just fine, there are more effective uses for the onLoad event handler than just for an alert box. We'll take a look at them later in our studies.)

A better way to use the onLoad event handler would be to put it in an external file, with a link to the file placed in the head of the page. That way it can easily be changed and will not be mixed with the HTML code. Actually, you should place all of your JavaScript code in an external file. You'll need to make sure that there are no duplicate function names or variable names so as to eliminate any problems. Place the onLoad event handler at the end of the file:

window.onload=functionName;

If you have more than one onLoad event handler, you will need to call them a little differently. If you were to just list them all as the example above, only the last one would execute, e.g.:

window.onload=functionName1;
window.onload=functionName2;
window.onload=functionName3;

In the example above, only the window.onload=functionName3; would run. This is because each succeeding line would replace the previous one. To run more than one onLoad event handler, you need to list the calls within a function. One way of doing that is shown below:

function start() {
  runFunction1();
  runFunction2();
  runFunction3();
}
window.onload = start;

In the example above, once the window has loaded, the function named start will execute and run the other functions listed. (Be sure to include a link to the external file in the head section of page.)

home / programming / javascript / diaries / 9

[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Whitepapers and eBooks

Symantec Whitepaper: Converging System and Data Protection for Complete Disaster Recovery
Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
  Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Symantec Whitepaper: Comprehensive Backup and Recovery of VMware Virtual Infrastructure
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle

Created: September 16, 2005

URL: