CancelImage Upload

Defining an action for yes, no, accept or cancel replies in a JavaScript Confirm Box

When defining a JavaScript Confirm Message Box an action needs to be set for each reply. This is accomplished by creating a variable through which JavaScript can tell which response was chosen.

In the following code for example the user is asked after having chosen an action, whether or not he (or she) is really sure they want to carry it out. A good use for this kind of prompt would be in combination with any "Are you sure you want to destroy the world?" self destruct buttons. If the defined variable ends up containing a yes response we can then initialise the function. If not we will simple state that the action has been cancelled:

<script type="text/javascript">
<!--

var promptBox = confirm("Are you sure you want to perform this action?")

if(promptBox)
{

      //Define action function here

}
else
{

      alert ("The action has been cancelled.")

}

// -->
</script>

As explained in the above example the answer is loaded into the variable promptBox. With an if else statement we then decide upon our course of action. If a simple negative statement is needed we could simple place an ! in front of the variable inside the if statement:

<script type="text/javascript">
<!--

var promptBox = confirm("Are you sure you want to perform this action?")

if(!promptBox)
{

      //Define action function here

}

// -->
</script>

And that is all there is to it!

Login
Want to leave a comment?

No problem. Just enter your email and password below.


register | home | reminder

myDesignTool Networking • www.mydesigntool.cominfo@mydesigntool.com