w3resource logo

JavaScript Window Object - confirm Method

JavaScript Window Object : confirm() Method

Secondary Nav


The confirm() method of window object is used to displays a confirm dialog box with specified message and two buttons OK and Cancel.

Use the confirm method to ask the user to make a decision that requires either an OK or a Cancel. The message argument specifies a message that prompts the user for the decision. The confirm method returns true if the user chooses OK and false if the user chooses Cancel. You cannot specify a title for a confirm dialog box, but you can use the open method to create your own confirm dialog. See open.


Implemented in JavaScript 1.0




message : A string.

Pictorial presentation of confirm dialog box

javascript alert

Example of Window object : confirm() method

The following web document demonstrates how the confirm() method can be used.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>JavaScript window object - confirm() method example</title> </head> <body> <h1 style="color: red">JavaScript window Object : confirm() method</h1> <h3>Click the following button to get the message.</h3> <hr /> <script type="text/javascript"> //This is done to make the following JavaScript code compatible to XHTML. <![CDATA[ function userresponse() { result = confirm("Select a Button") if (result == true) { alert("You select Ok...") } else { alert("You select Cancel...") } } //]]> </script> <input type = "button" value = "Click here to test.." onclick="userresponse()" /> </body> </html>

View the example of window object's confirm method in the browser

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

See also :

JavaScript Client side objects, methods, properties.

JavaScript Core objects, methods, properties.

Is this content useful for you?


You might be using Adblocker. Since w3resource does not charge anything from users, all of our expenses (e.g. Hosting, Content Creation, Development etc.) are met by advertisement. Please whitelist w3resource.