X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

Window confirm() Method

Window Object Reference Window Object

Example

Display a confirmation box:

confirm("Press a button!");

Try it yourself »


Definition and Usage

The confirm() method displays a dialog box with a specified message, along with an OK and a Cancel button.

A confirm box is often used if you want the user to verify or accept something.

Note: The confirm box takes the focus away from the current window, and forces the browser to read the message. Do not overuse this method, as it prevent the user from accessing other parts of the page until the box is closed.

The confirm() method returns true if the user clicked "OK", and false otherwise.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The confirm() method is supported in all major browsers.


Syntax

confirm(message)

Parameter Values

Parameter Description
message Optional. Specifies the text to display in the confirm box. This parameter is required in Firefox.

Return Value

Type Description
Boolean A boolean value indicating whether "OK" or "Cancel" was clicked in the dialog box:
  • true - the user clicked "OK"
  • false - the user clicked "Cancel" or the "x" (close) button that is available in all major browsers, except Firefox.


More Examples

Example

Display a confirm box, and write what the user clicked:

var txt;
var r = confirm("Press a button!");
if (r == true) {
    txt = "You pressed OK!";
} else {
    txt = "You pressed Cancel!";
}

Try it yourself »

Example

Confirm box with line-breaks:

confirm("Press a button!\nEither OK or Cancel.");

Try it yourself »


Window Object Reference Window Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates FREE Website Templates Free HTML Templates

SHARE THIS PAGE

facebook