In the previous lesson we learned how to make an alert button. However, you do not need a button to do an alert! Click here. to see.
Here is the code:
<a href="index.html" onclick="alert('Don\'t forget to press the back button to return!')">
This code starts off just like any hyperlink with <a HREF="index.html" and just like the previous button it has
onclick="alert('Don\'t forget to press the back button to return!')"
The only thing funny about this one is the strange Don\'t! The \ tells the browser that this apostrophe (') is part of text and not part of the program. So remember, when you have words with apostrophes or quotations in your message, put a \ before the apostrophe or quotation mark to tell the browser this is text and not part of the program! This is important because even when you cut and paste, without knowing about the code, you often have to add text. If you add a word with an apostrophe or quotation mark, then you may get a Java Script error, if you don't use the \. You might think there is something wrong with the program when it is your text that is causing the problem!
Now click here.. When I click this I get the same message as before, but I don't go anywhere when I click OK. Here is the code:
<a href="index.html" onclick="alert('Don\'t forget to press the back button to return!');return false">
The only addition is to write return false. What this does is to prevent the browser from acting on the URL. It uses a real hyperlink, but does not act on it!
Next: Alerts with images
|[back to: alerts]
[Contents of this Site][contents of Java Script Tutorial]
[on to: Alerts with images]
[If you need to learn more about HTML visit the HTML Tutorial]
Most Recent Revision: 18-Oct-98.
I am always pleased to hear from you.