Online Software Educational Training - IT Tutorials - Online Education Training for Computer Software


Home

JAVA SCRIPT

Link Events

 

Whenever a user clicks on a link, or moves her cursor over one, JavaScript is sent a link event. One link event is called onClick, and it gets sent whenever someone clicks on a link. Another link event is called onMouseOver. This one gets sent when someone moves the cursor over the link.

You can use these events to affect what the user sees on a page. Here's an example of how to use link events. Try it out, View Source, and we'll go over it line by line.

The first interesting thing is that there are no <script> tags. That's because anything that appears in the quotes of an onClick or an onMouseOver is automatically interpreted as JavaScript. In fact, because semicolons mark the end of statements allowing you to write entire JavaScripts in one line, you can fit an entire JavaScript program between the quotes of an onClick. It'd be ugly, but you could do it.

Here's the first line of interest:

<a href="#" onClick="alert('Ooo, do it again!');">Click on me!</a>


This is just like a normal anchor tag, but it has the magic onClick="" element, which says, "When someone clicks on this link, run the little bit of JavaScript between my quotes." Notice, there's even a terminating semicolon at the end of the alert.

Also note that there's nothing between the quotes in the href="". This makes it so the page doesn't go anywhere when you actually click on the link.

The next line is

<a href="#" onMouseOver="alert('Hee hee!');">Mouse over me!</a>


This is just like the first line, but it uses an onMouseOver instead of an onClick.

Now that we've learned about link events, we can go into the wonderful land of image swaps.

<Previous                                                                                                      Next>