AlfieWebDevTips Homefor new and experienced developers
[Home Home] [Web Development Web Development ] [Code Generators Code Generators ] [Resources Resources ] [Contact Us Contact ] |WebDevTips Privacy Privacy |
Print This Page

You are here : Home - Web Development - Javascript - Alert Guide

Javascript Alert Guide



Example alert dialog

Alert boxes are used to provide additional information and are commonly used to warn the visitor that a form hasn't been filled in properly.

Alert Icon

Alert boxes contain an Icon with an ! similar to the one above. The title bar of the alert varies according the the browser used and cannot be changed.

Alert boxes only have 1 button with OK on it. Again this cannot be changed.

Clicking the OK button returns nothing to the browser, effectively clicking the OK button closes the dialog.

The only part of an alert box you have control over is the text. Syntax for an alert is simple.
alert("whatever message you want"); - (script tags omitted)

Here is a full syntax example - Click here to test alert

and the code

<a href="alert.shtml" onClick="alert('this page will now reload');">Click here to test alert</a>

If you want more than 1 line of text then all you have to do is use \n where you want the new line to start. For example
alert("whatever \nmessage \nyou \nwant");

Generated strings can also be sent to the alert box and displayed like this. 3 line alert

The code

<script language="JavaScript" type="text/javascript"> 
<!-- 
var alert_msg = ""; // set as global variable 
alert_msg += "This is line 1"; 
alert_msg += "\nThis is line 2"; 
alert_msg += "\nThis is line 3"; 
// -->
 
</script> 
 
<a href="#" onClick="alert(alert_msg);return false;">3 line alert</a>

Notice that alert(alert_msg); there are no " " surrounding alert_msg and that alert_msg matches the string name in the javascript. There's nothing to stop you creating multiple strings and calling them from the alert.

For example

View my first message
View my second message
View my third message

The code

<script language="JavaScript" type="text/javascript"> 
<!-- 
var my_msg_1 = "This is my first message"; 
var my_msg_2 = "This is my second message"; 
var my_msg_3 = "This is my third message"; 
// -->
 
</script> 
<a href="#" onClick="alert(my_msg_1);return false;">View my first message</a><br> 
<a href="#" onClick="alert(my_msg_2);return false;">View my second message</a><br> 
<a href="#" onClick="alert(my_msg_3);return false;">View my third message</a><br>

So that's about all there is to the alert box.
Next we'll look at the prompt box.


intro | alert | prompt | confirm | final chapter





Google
 

[back to top of page]   

[labelled with icra -  - this link will open in a new window] [RSS Content]

[Copyright © WebDevTips]