Javascript TUTORIAL


Javascript Confirm 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 or reject the request. The confirm statement takes a string that will be displayed with the text box along with the OK and the Cancel Button. In addition it will return a value. The returned value will be true if OK button is pressed. The returned value will be false if Cancel button is pressed. We can use a variable and an if statement to determine if the 'OK' or 'CANCEL' button is clicked..

Let us look at the example that simply displays a confirm box and diplays an alert box with a message "You said :OK" if the user presses OK button. It will display an alert box with a "You said : Cancel", if the user presses Cancel Button.

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Example confirm Box
********************************************************
*/
var answer = confirm("Do you want continue ?");
if (answer)
alert("You said: Ok");
else
alert("You said: Cancel");
//-->
</script>
</body>
</html>



You can try this example online at the link below, which will open in a new window.

Javascript confirm Box Example

Confirm Box - Another Example - Directing to another link


Here is another example which displays a confirm box and asks user if he wants to get directed to another news link. If the user presses OK it takes to a news link.

<html>
<body>
<script type="text/javascript">
<!--
/*
********************************************************
Example confirm box - directing to another link
********************************************************
*/
var answer = confirm("Do you want to search something in google ?");
if (answer)
window.location = "http://www.google.com";
else
alert("You said: Cancel");

//-->
</script>
</body>
</html>


window.location makes you jump to another web link assigned to it.

You can try this example online at the link below, which will open in a new window.

Javascript confirm Box - Redirect to another location Example