Window Confirm Method

How to popup a message and get user confirmation in javascript?


Object: Window
Method 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.


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>
<script language=javascript>
var df = confirm(" can we change the size to 4");
if(df) = 4;
else = 20;


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.

Ask Questions

Ask Question