A javascript alert box is a dialog box that pops-up with a message and an 'OK' button. The alert method displays an alert box with a string passed to it. For example: alert() will display an empty dialog box with just the 'OK' button. alert("Hello world") will display a dialog box with the message, 'Hello world' and an 'OK' button. (it remains paused until it is clicked). You can display any type of value on an alert box.
For example:
var number = 12345
alert("The number is: "+number)This simply displays, 'The number is: 12345' on an alert box. The '+' sign ties the two values that are to be displayed in one statement.
Here is a javascript alert box that pops-up when the page is loaded:
Here is a javascript alert box that pops-up when a link is clicked:
Here is a javascript alert box that pops-up when a button is clicked:
The prompt() is a method of the window object, just like alert() we just saw. The format for prompt() is similar to alert(), except for one addition. Prompt uses a text field to enter a value. It also has 'OK' and 'CANCEL' buttons, while the alert has only one button.
Here is an example of a prompt box:
prompt("Type your name:"). This prompt method will display the message, 'Type your name' on a prompt box and text field with the default text of undefined. We can display default text in the text field by simply doing the following:
prompt("Type your name:", "Type here")
The information submitted to the prompt() can be stored in a variable as follows:
var name = prompt("Type your name:", "Type here")
Once we have a value from the prompt box stored in a variable, we can display it on a message box.
Here is a javascript prompt box that displays the provided value on a message box:
The Javascript confirm box differs from a regular alert box in that it provides two choices for the user: 'OK' and 'CANCEL' to confirm the request. You can use a variable and an if statement to determine if the 'OK' or 'CANCEL' button is clicked.
Here is an example of a confirm box:
Confirm: ("Do you want to continue?") This displays a dialog box with the message, 'Do you want to continue?' (with 'OK' and 'CANCEL').
Here is a javascript confirm box that tells you which button you clicked:
Here is an example that directs the user to another page when the, 'OK' button is clicked:
For example:
var number = 12345
alert("The number is: "+number)This simply displays, 'The number is: 12345' on an alert box. The '+' sign ties the two values that are to be displayed in one statement.
Here is a javascript alert box that pops-up when the page is loaded:
<html> <body> <script type="text/javascript"> alert("Hello world") </script> </body> </html> | Hello world is displayed in a dialog box when the page is loaded. |
Here is a javascript alert box that pops-up when a link is clicked:
<html> <body> <A HREF='javascript:onClick=alert("Not an active link")'>Click here</A> </body> </html> | onClick is an event that understands the mouse click. We set the onClick event to the alert box so that when the active link is clicked, the onClick event executes the alert box. Here is the result of this example: asdhfasd |
Here is a javascript alert box that pops-up when a button is clicked:
<html> <body> <form> <input type="button" name="click" value="Click To Alert" onclick='alert("You clicked a button to display an alert box")'> </form> </body> </html> | Again, we used the onClick event to display the alert box when the button is clicked. Here is the result of this example: |
Javascript Prompt Box
Here is an example of a prompt box:
prompt("Type your name:"). This prompt method will display the message, 'Type your name' on a prompt box and text field with the default text of undefined. We can display default text in the text field by simply doing the following:
prompt("Type your name:", "Type here")
The information submitted to the prompt() can be stored in a variable as follows:
var name = prompt("Type your name:", "Type here")
Once we have a value from the prompt box stored in a variable, we can display it on a message box.
Here is a javascript prompt box that displays the provided value on a message box:
<html> <body> <script type="text/javascript"> var name = prompt("What is your name?", "Type your name here"); alert("Your name is: "+name) </script> </body> </html> | This example displays a prompt box when the page is loaded with the message, 'What is your name?' and the default text field value of, 'Type your name here'. Once a name is provided, it is stored in the variable name, and is displayed on an alert box. |
Javascript Confirm Box
Here is an example of a confirm box:
Confirm: ("Do you want to continue?") This displays a dialog box with the message, 'Do you want to continue?' (with 'OK' and 'CANCEL').
Here is a javascript confirm box that tells you which button you clicked:
<html> <body> <script type="text/javascript"> var answer = Confirm: ("Do you want continue?") if (answer) alert("You said: Ok") else alert("You said: Cancel") </script> </body> </html> | We stored the confirm objects in the variable, answer, and then used an if statement to determine which button was clicked. The default and expected button is, 'Ok' so we stated if the answer is equal to 'Ok', then go to a specify location. Else will be "true", and the second message will be displayed, if the current value of answer is not the default value. |
Here is an example that directs the user to another page when the, 'OK' button is clicked:
<html> <body> <script type="text/javascript"> var answer = Confirm: ("Do you want a different page?") if (answer) window.location = "http://www.videogamedeals.com" else alert("Nothing happened") </script> </body> </html> | We stored the confirm objects in the variable answer, and then used an if statement to determine which button was clicked. The default and expected button is, 'Ok' so we stated that if the answer is equal to 'Ok', then go to: www.videogamedeals.com. Else will be "true", and the second message, 'Nothing happened' will be displayed if the current value of answer is not the default value. |
Không có nhận xét nào:
Đăng nhận xét