JavaScript AlertsAdding javascript alerts to surprise peopleHome/JavaScript/Basics/Alerts
Well, you want to add one of those JavaScript alert boxes that come out of nowhere, don't you? Okay, let's begin with the alert box that just tells the viewer something you want them to know. Here's the alert command: alert('your choice of text') Now, to use it, we'll need to place it inside another command. Let's use one we are familiar with, the onMouseover command. The following script will alert the user that you did not want them trying to click this particular link when they move their mouse over it. <A HREF="noplace" onMouseover="alert('Hey! I said not to try clicking this link!')"> Don't click this link!</A> Give it a try. Move your mouse over the link below: Yes! Now you can drive your friends insane with a bunch of fake links! Here's what all the commands are doing:
That was nice, but could you make something a little more annoying? Of course! It's called the "alert as soon as you open the page!" alert. Just think, you could tell people stuff before they ever see anything on the page! The trick: placing the alert command inside the <HEAD> </HEAD> tags! You will now get to use the old SCRIPT tags I mentioned a couple of sections ago to set off the JavaScript from the other stuff. Here is the code:
<HEAD> This will display the alert before the page starts loading. When you hit "OK" the page will go on and load normally. Here's the breakdown:
Do you want to try it out? Click on the link to see the alert before the new page. So, do you want to get carried away? Place several of these alerts inside the SCRIPT tag, following each with a semicolon. The viewer will have to say "OK" to every alert before the page will begin loading. Try it out yourself and see if you go insane. Click the link below! Want the code that did this? Here you go!
<HEAD> So, how about that? Pretty wild, isn't it? You can also use the alert with a button, so that it is not such a surprise. Place the code where you want the button to be on your page. You won't need the SCRIPT tag for this one.
<FORM> Give it a try. You'll be glad you did....I think.....
Well, that wraps up this section. Let's Go on to Variables and Functions.
Other Topics: ASP/PHP | DHTML | Java The tutorials and articles on these pages are © 1997-2010 by John Pollock and may not be reposted without written permission from the author, and may not be reprinted for profit. Disclaimer. |
|
By: John Pollock |
|