on the OK button, the confirm() will return true. If the user clicks on the Cancel button, then confirm() returns false. The syntax of confirm dialog box is confirm(“message”); (or) window.confirm(“message”); Example: confirm(“Hai Do you want to continue:”); (or) window.confirm(“Hai Do you want to continue:”); <Html> <Head> <Title>Demo Program - To test Confirm Dialog Box in JavaScript </Title> </Head> <Body> <script language="javascript" type="text/javascript"> var value1 = , value2= ; window.confirm("Data1 : "+value1); confirm("Data2 :"+value2); </script> </Body> </Html> Illustration . Confirm Dialog Box Chapter Pages Output: .
. Prompt Dialog Box: The prompt dialog box is very useful when the user want to pop-up a text box to get user input. Thus, it enables you to interact with the user. The user needs to fill in the text box field and then click OK.
The prompt dialog box is displayed using a method called prompt() which takes two parameters: (i) a label which you want to display in the text box and (ii) a default string to display in the text box. This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the prompt() will return the entered value from the text box. If the user clicks the Cancel button, the prompt() returns null.
The Syntax of prompt dialog box is, Prompt(“Message”,”defaultValue”); (or) window.prompt("sometext","defaultText"); Example: prompt(“Enter Your Name:”,”Name”); (or) window.prompt(“Enter Your Name:”,”Name”); <Html> <Head> <Title>Demo Program - To test Prompt Dialog Box in JavaScript </Title> </Head> <Body> <script language="javascript" type="text/javascript"> var sname = prompt("Please enter your name", "Name"); </script> </Body> </Html> Illustration . Prompt Dialog Box Chapter Pages Output: . Comments in JavaScript: Very important aspect of good programming style is to insert remarks and commentary directly in source code, making it more readable to yourself as well as to others. Any comments you include will be ignored by the JavaScript interpreter.
There are two types of comments, Single line and Multiple lines comments. Single-line comments begin with a double slash (//), causing the interpreter to ignore everything from that point to the