|
Writing HTML | About | FAQ | References | Tags | Lessons | previous | next |27a. JavaScript : Alerts and Rollovers You've tasted it... ObjectivesAfter this lesson you will be able to:
LessonNote: If you do not have the working documents from the previous lessons, download a copy now. Our first dose of JavaScript is to write a command inside a hyperlink tag to do something other than jump to a URL when it is clicked. This allows our links to do more than just transfer us to another document: <a href="alt.html" onClick="JavaScript Statement; return false">link text</a> NOTE: The listed JavaScript statement will be processed when the viewer clicks the hypertext link. The onClick="..." event is triggered by a mouse click. Note the "C" must be capitalized! The first thing we will do is to modify our terminology page of your Volcano Web project, so that the list of words in the first paragraph display an alert message defining their meaning.
Look carefully at the way quote characters are used in the onClick part of the tag. The entire JavaScript command must be enclosed in a set of quotes. And the alert command itself must include another string that is in quotes. We use single quotes and double quotes for this purpose, and it does not matter which one we use: <A HREF="#" onClick="alert('You are the boss!')">tell me something</A> will act exactly like: <A HREF="#" onClick='alert("You are the boss!")'>tell me something</A> Why bother? Let's say that the message you want to display in the JavaScript triggered alert needs to contain a quote as a character. JavaScript needs to know that the quote means just a quote and not part of the JavaScript instruction. We can do this by putting the forward slash (\) in front of the quote character' known in techie terms as "escaping" the character. In our example above, we want the words Look Out! in the last list item to have quotes around them. If we want these to be single quotes, we would use: <li><A HREF="#" onClick="alert('A lahar is a mudslide generated from the flanks of a volcano. Some say it comes from the phrase \'Look Out!\' in the Indonesian language.')">lahar</a> but since we wanted double quotes, we swapped our uses of single and double quotes like: <li><A HREF="#" onClick='alert("A lahar is a mudslide generated from the flanks of a volcano. Some say it comes from the phrase \"Look Out!\" in the Indonesian language.")'>lahar</a> Finally, we need to create 5 new (but small) HTML pages that will be used if the person viewing our page cannot use the JavaScript command.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <html> <head> <title>Volcano Terminology</title> </head> <BODY BGCOLOR=#000000 TEXT=#FFFF00 LINK=#33CCFF VLINK=#FF6666> <h2 align=center> <font color="#FF0000">Vesicularity</font> is a measure how much of a rock volume consists of air chambers. </h2> <center> <a href="term.html">return</a> </center> </body> </html> Some MouseOver Action!So far we have learned how to use JavaScript to do something when the viewer clicks a hypertext link. We can add another feature to our hyperlinks to do something just when the viewer moves their mouse over the linking text (without clicking the link). The normal behavior for this action is that the web browser displays in its status bar (the area usually at the bottom left corner of the browser window) the URL that the link leads to, like what you see if you move your mouse over, but do not click on, this link to our tutorial. With JavaScript, we can create a custom message that is displayed instead of the link's URL, like this second link to our tutorial. Clicking either of this links will do the expected; transfer you to the web site specified in the hyperlink's URL. The JavaScript format for adding this feature is: <a href="xxxx.html" onMouseOver="window.status='text of custom message'; return true">linking text</a> The JavaScript event that triggers the custom message is onMouseOver="..." meaning the hypertext link says "when the mouse is over me, do this". There are two different JavaScript function statements here, separated by a semi-colon (;).
Now, we will add a mouseOver message to the links we modified for the terminology web page that produce the alert messages.
<ul> <li><A HREF="term_1.html" onClick="alert('A caldera is a circular shaped landform depression caused by the eruption of a large, near surface body of magma.'); return false" onMouseOver="window.status='what is a caldera?'; return true">caldera</A> <li><A HREF="term_2.html" onClick="alert('Vesicularity is a measure how much of a rock volume consists of air chambers.'); return false" onMouseOver="window.status='what is vesicularity?'; return true">vesicularity</a> <li><A HREF="term_3.html" onClick="alert('Pahoehoe is a type of basaltic lava flow texture that comes from the Hawaiian word for smooth and ropy.'); return false" onMouseOver="window.status='what is pahoehoe?'; return true">pahoehoe</a> <li><A HREF="term_4.html" onClick="alert('Rheology is the study of how materials deform.'); return false" onMouseOver="window.status='what is rheology?'; return true">rheology</a> <li><A HREF="term_5.html" onClick='alert("A lahar is a mudslide generated from the flanks of a volcano. Some say it comes from the phrase \"Look Out!\" in the Indonesian language."); return false' onMouseOver="window.status='what is a lahar?'; return true">lahar</a> </ul> NOTE: If you would like to copy and paste this code, use this sample of HTML that will load in a new browser window. If your JavaScript is working, as you move the mouse over the image on this page, the status bar will display the custom message that describe the link, rather than the URL. NOTE: As of April 1999, this technique of using onMouseOver inside the <map>...</map> tags does not work on Microsoft Internet Explorer 5.0 web browsers. Using the mouseOver technique can be an effective feature of your web site, but keep in mind that sometimes it is more useful for the person looking at your site to know the URL where the link leads to (perhaps so they can guess the organization that owns the web site from its URL)-- the onMouseOver message hides this information. Use it where it makes sense for your web site design and what you can determine are the needs of the visitors to your site. Check Your WorkCompare your web pages with this sample of how it should appear. If your pages are different from the sample or the hypertext links do not work correctly, review the text you entered in the text editor. Note that JavaScript is very sensitive to typographical mistakes -- one missing quote or semi-colon can ruin the page! ReviewReview topics for this lesson:
Independent PracticeAdd some JavaScript alert and mouseOver code to some of the hyperlinks in your own web pages. Extra Credit: Can you think of a way that a mouseOver can generate an alert message? Coming Next....Get a second dose of web medicine from your JavaScript Pharmacy... JavaScript that creates custom content and dynamic footers. GO TO.... | Lesson Index | previous: "JavaScript: Intro" | next: "JavaScript: Dynamic Content" |
Writing HTML: Lesson 27a: A Wee Dose of JavaScript : Alerts and Rollovers
|