Sams Teach Yourself JavaScript in 24 Hours

Sams Teach Yourself JavaScript in 24 Hours

By Michael Moncur

Q&A

Q1:

I noticed that the <img> tag in HTML can't have onMouseOver or onClick event handlers in some browsers. How can my scripts respond when the mouse moves over an image?

A1:

The easiest way to do this is to make the image a link by surrounding it with an <a> tag. You can include the BORDER=0 attribute to prevent the blue link border from being displayed around the image. You'll see an example of this in Hour 13.

Q2:

My image rollovers using onMouseOver work perfectly in Internet Explorer, but not in Netscape. Why?

A2:

Re-read the previous answer, and check whether you've used an onMouseOver event for an <img> tag. This is supported by Internet Explorer and Netscape 6, but not by earlier versions of Netscape.

Q3:

What happens if I define both onKeyDown and onKeyPress event handlers? Will they both be called when a key is pressed?

A3:

The onKeyDown event handler is called first. If it returns true, the onKeyPress event is called. Otherwise, no keypress event is generated.

Q4:

When I use the onLoad event, my event handler sometimes executes before the page is done loading, or before some of the graphics. Is there a better way?

A4:

This is a bug in some older versions of JavaScript. One solution is to add a slight delay to your script using the setTimeout method. You'll learn how to use this method in Hour 11, "Using Windows and Frames."

Q5:

When I try the descriptive links example in Netscape 6.2, the descriptions don't display. Why not?

A5:

At this writing, the latest version of Netscape 6 fails to display the descriptions if your link includes the HREF attribute. One way around this is to use an onClick event handler instead, as in <a onClick="location.href='newpage.html'">. This works, but Netscape doesn't display it in the correct link color.

Share ThisShare This

Informit Network