Suggested solutions to these questions can be found in Appendix A.
Create a page with a number of links. Then write code that fires on the window onload event, displaying the href of each of the links on the page. |
||
A: |
<html> <head> <script language=JavaScript> function displayLinks() { var linksCounter for (linksCounter = 0; linksCounter < document.links.length; linksCounter++) { alert(document.links[linksCounter].href); } } </script> </head> <body onload="displayLinks()"> <A href="link0.htm" >Link 0</A> <A href="link1.htm">Link 2</A> <A href="link2.htm">Link 2</A> </body> </html> Save this as ch05_q1.htm. We connect to the window object's onload event handler by adding an attribute to the <body> tag.
<body onload="displayLinks()">
On the onload event firing, this will run the script in quotes calling the displayLinks() function. In this function we use a for loop to cycle through each A object in the document object's links array. function displayLinks() { var linksCounter for (linksCounter = 0; linksCounter < document.links.length; linksCounter++) { alert(document.links[linksCounter].href); } } We used the length property of the links array in our condition to determine how many times we need to loop. Then, using an alert box we display each A object's href property. We can't use document.write() in the onload event, because it occurs when the page has finished loading. |
Insert an image in the page with the <img> tag. When the mouse pointer rolls over the image, it should switch to a different image. When it rolls out (leaves the image), it should swap back again. You'll need to wrap the images inside <A> tags because img objects don't have the onmouseover and onmouseout events. |
||
A: |
<html> <head> <script language=JavaScript> function mouseOver() { document.images["myImage"].src = "Img2.jpg"; } function mouseOut() { document.images["myImage"].src = "Img1.jpg"; } </script> </head> <body> <A style="cursor : default" href="" onclick="return false" name="link1" onmouseover="mouseOver()" onmouseout="mouseOut()"> <img src="Img1.jpg" name="myImage"> </A> </body> </html> Save this as ch05_q3.htm. At the top of the page we define our two functions to handle the onmouseover and onmouseout events. function mouseOver() { document.images["myImage"].src = "Img2.jpg"; } function mouseOut() { document.images["myImage"].src = "Img1.jpg"; } The function names tell us what event they will be handling. We access the img object for our <img> tag using the document.images array and putting the name in square brackets. In the onmouseover event we change the src property of the image to Img2.jpg, and in the onmouseout event we change it back to img1.jpg, the image we specified when the page was loaded. In the page itself we have our <img> tag inside an <A> tag. <A style="cursor : default" href="" onclick="return false" name="link1" onmouseover="mouseOver()" onmouseout="mouseOut()"> <img src="Img1.jpg" name="myImage"> </A> We need to do it this way so that our pages work with NN 3 and 4. Only IE 4/5/6 and NN 6/7 support the onmouseover and onmouseout events for an img object. However, NN 4 does support these events for the <A> tag's A object, so we use this workaround to get the result we want. Note that I've also added onclick="return false". Why? By returning false, the click event is canceled; remember this link is here for its events only and not as a hyperlink. Why not leave the href="" out altogether? Well, while this will work fine in IE, it won't in NN, which ignores events if no href is included. Finally, for IE only, I've set the cursor style when the mouse pointer is over the link to the default cursor rather than a hyperlink mouse cursor.
style="cursor : default"
|