Confirm dialog in JavaScript

JavaScript FAQ | JavaScript Dialogs FAQ  

Question: How do I display an OK/Cancel dialog box from JavaScript?

Answer: To display an OK/Cancel box, use the confirm() method:

if (confirm('Your question')) { 
 // do things if OK

The button above was created using the following code:

<form name=myform>
<input type=button value="Try it now" 
onClick="if(confirm('Format the hard disk?'))
alert('You are very brave!');
else alert('A wise decision!')">

Returned value:
If the user clicks OK, the confirm method returns true. If the user clicks Cancel, the method returns false.

See also other JavaScript dialogs:

  • Alert (message box) dialog
  • Prompt dialog
  • Print dialog
  • Find dialog
  • Add Favorite dialog
  • Copyright © 1999-2011,