/

HTML / CSS / JavaScript Tutorial

JavaScript Window method: confirm()

[this page | pdf | back links]

The confirm() method (when applied to Window objects in the JavaScript BOM) displays text in a dialog box (with an OK and Cancel button).

 

It has the following syntax with the following parameters. It returns true if the user clicks the OK button, otherwise false.

 

window.confirm(message)

 

Parameter

Required / Optional

Description

message

Optional

String specifying text to display in dialog box

 

EXAMPLE:


HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
<button onclick="myOnclick()">Open a confirm box</button><br>
<span id="x1"></span>

<script>
function myOnclick() {
  var x = confirm("Press OK or Cancel");
  var y;
  if (x == true) { y = "OK"; } else { y = "Cancel"; }
  document.getElementById("x1").innerHTML = "You pressed: " + y
}
</script>

</body>
</html>

FUNCTION THAT MAY ASSIST IN TESTING WHETHER FEATURE IS SUPPORTED:
function isSupportedJavaScriptMethodWindowConfirm() {
  return !!window.confirm;
}


NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)


Desktop view | Switch to Mobile