Her

Home Web Programming JavaScript Creating Alerts

Creating Alerts

Author: Kreate^ Author's URL: www.avengex.com More by this author

Creating AlertsSo you've seen those annoying websites that have alert boxes appearing every time you click or open something and wondered how to do it? Well here's your answer.

Firstly, your basic command for an alert box in javascript is

alert('This is an alert box!')

The above code will produce an alert box with the text "This is an alert box!" inside it.

So, how do you use it?

Well first of all, let's try making an alert appear when you open a web page. In the <head> of your (X)HTML page, add the following:

<script language="JavaScript">
alert('Welcome to my site!');
</script>

Now, when you open your webpage, the alert box will come up and the user must press the OK button before the rest of the site loads.

However, you are not just limited to one alert box! You can have as many as you like! How? Just add more alerts! For example:

<script language="JavaScript">
alert('Hello!');
alert('Welcome to my brand new website!');
alert('Remember to check out the forums!');
alert('And please sign the guestbook!');
alert('I hope you enjoy your visit!');
</script>

The user will have to click OK to all of these alerts before the page loads.

Another use for the alert box is when the user hovers their mouse over something on your page. This is how to do it:

<a onMouseOver="alert('The forums are still under construction!')">Forums</a>

The user will not be able to click this link as everytime they move their mouse over it, the alert box will appear and they cannot do anything else on your page until they have clicked the OK button to close the alert box.