JavaScript Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
Java-Script Tutorial
Introduction Introduction
Variables Variables
Operators Operators
Statements Statements
Loops Loops
Functions Functions
Objects Objects
Arrays Arrays
Cookies JS Cookies
Browser Objects Browser Objects
Form DOM Form DOM
Style Objects DOM Style Objects
Forums Ask Your Doubts
Scraps More about Javascript
Feedback Feedback
 




Window Confirm Method


Tutorials Javascript

Topic

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



Explanation

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. 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.





  • A Note
    Javascript (JS) is one of the most used languages in the world. Some times spelled as Java Script. Hope you enjoy this tutorial. Do send your feedback or suggestions on this javascript or java script tutorial. This is a copyright content.

    Other Links

    web hosting