Electric Type

Multimedia

About Us

News

Help

How to Steal JavaScript

Page 4 — Events and Event Handlers

In JavaScript, an event occurs whenever something happens on a Web page. Events occur when the page finishes loading or when users click on a link or move their cursors over images. The cool thing about events is that you can specify what JavaScript should run when a given event happens. For example, you could have an image change whenever the cursor moves over it or you could select a random background color to appear each time a page loads (as in our example).

The most common way to specify what happens when an event occurs is to embed an event handler in an HTML tag, as we did in our random background color example:

<body onLoad="setBackgroundColor('#ff0000','#00ff00','#ffff00','#3333ff');">  

In this example, onLoad is an event handler because it tells the JavaScript interpreter how to handle the load event, which occurs whenever the body loads (because it's in the HTML body tag).

When the body loads, this line of code will call the setBackgroundColor function, which will then randomly pick a new color for the background. JavaScript authors frequently use the onLoad handler in the body tag to run a function whenever the page loads because this is a particularly handy time to get things done (since the user is already waiting patiently).

There are a lot of different event handlers, but here are the ones you'll run into most often:

<body onLoad="someFunction();">
<a href="#" onMouseOver="someFunction();"
onMouseOut="anotherFunction();"
onClick="yaFunction();">
<a href="javascript: someFunction();">
<form onSubmit="someFunction();">
<input type="button" onClick="someFunction();">
<select onChange="someFunction();">

That's about it. You now have everything you need to know to make a burgled JavaScript work on your own pages. Now let's put it all together.

next page»


Dynamic HTML  

Frames  

HTML Basics  

Stylesheets  

Tables  

XML  

Javascript  

Database Connections  

Intro To Perl  

HTML 4.0  

User Blogs

Screen Shots

Latest Updates

Contact Us

Valid HTML 4.01!
Valid CSS!

Breadcrumb

© ElectricType
Maintained by My-Hosts.com
Site map | Copyright | Disclaimer
Privacy policy | Acceptable Use Policy
Legal information.