JavaScript Alert Box
In this JavaScript tutorial, you will learn about JavaScript alert box along with code for placing the alert box when page is loaded.
There are three types of popup boxes that can be created using JavaScript. Depending on the needs of user, the programmer can create any of these types of popup boxes using JavaScript. Three kinds of popup boxes created using JavaScript are:
- Alert Box
- Confirm Box
- Prompt Box
Let us now learn how to create each of these popup boxes using JavaScript.
Alert Box:
This type of popup box is a dialog box used when a programmer wants to make sure that the information is passed to the user. The alert box pops up with an OK button which the user has to press to continue further.
General syntax of alert box is
alert(“textmessage”)
For example:
alert(“Exforsys”)
The above statement will display a dialog box with message Exforsys and a OK button.
The dialog box remains in view until the user presses the OK button.
When no text message is placed
alert()
This would display an empty dialog box with an OK button that the user must press to proceed.
It is also possible to display messages with values of variables in an alert box.
For example:
|
In the above script alert box displays the value of variable exam_score concatenated with a string.
The plus sign acts as a concatenation symbol.
Output of the above script as produced in a HTML page is shown below:
It is vital to understand that the alert box can be placed in different locations or events.
Depending on the placing of the alert box and based on the place of usage, the alert box will be activated.
For example,
- it is possible to place the alert box so that it pops up when a page is loaded.
- it is possible to place the alert box to be activated when a button is clicked.
Here is an example of JavaScript code for placing the alert box to be executed when a page is loaded.
|
Output of the above script as produced in a HTML page is shown below:
In the above example, message Exforsys pops up in the alert box when the page is loaded.