Window Confirm Method
How to popup a message and get user confirmation in javascript?
Explanation
Object:
WindowMethod or Function:
confirm(string)Description: This method is used to interact with user by popping up a message window and getting a confirmation. It takes the message as argument. Then shows a popup dialog box with the message and two buttons "ok" and "cancel".
If user selects "ok" it returns true, else return false. We can get the returned value and do our action.
Example:
For an example we will popup a message. If the user confirms, we will set the size of a text field as 4 else 10.
Example Code:
<form name=tett>
<input type=text name=tf value="testing confirm function" size=20>
</form>
<script language=javascript>
var df = confirm(" can we change the size to 4");
if(df)
document.tett.tf.size = 4;
else
document.tett.tf.size = 20;
</script>
Result:
Explanation:
In the above example,
- first we created a form named tett.
- form containing a text and button.
- in the javascript code, we popup a message using confirm function.
- the return value is captured in the variable named df.
- if user clicks "ok" df will be true and the if portion will be invoked and the size of text field is set to 4.