notification.confirm
Displays a customizable confirmation dialog box.
navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels])
-
message: Dialog message. (String)
-
confirmCallback: Callback to invoke with index of button pressed (1, 2, or 3) or when the dialog is dismissed without a button press (0). (Function)
-
title: Dialog title. (String) (Optional, defaults to
Confirm
) -
buttonLabels: Array of strings specifying button labels. (Array) (Optional, defaults to [
OK,Cancel
])
Description
The notification.confirm
method displays a native dialog box that is
more customizable than the browser's confirm
function.
confirmCallback
The confirmCallback
executes when the user presses one of the
buttons in the confirmation dialog box.
The callback takes the argument buttonIndex
(Number), which is the
index of the pressed button. Note that the index uses one-based
indexing, so the value is 1
, 2
, 3
, etc.
Supported Platforms
- Amazon Fire OS
- Android
- BlackBerry 10
- iOS
- Tizen
- Windows Phone 7 and 8
- Windows 8
Quick Example
// process the confirmation dialog result
function onConfirm(buttonIndex) {
alert('You selected button ' + buttonIndex);
}
// Show a custom confirmation dialog
//
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // message
onConfirm, // callback to invoke with index of button pressed
'Game Over', // title
['Restart','Exit'] // buttonLabels
);
}
Full Example
<!DOCTYPE html>
<html>
<head>
<title>Notification Example</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for device API libraries to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// device APIs are available
//
function onDeviceReady() {
// Empty
}
// process the confirmation dialog result
function onConfirm(buttonIndex) {
alert('You selected button ' + buttonIndex);
}
// Show a custom confirmation dialog
//
function showConfirm() {
navigator.notification.confirm(
'You are the winner!', // message
onConfirm, // callback to invoke with index of button pressed
'Game Over', // title
['Restart','Exit'] // buttonLabels
);
}
</script>
</head>
<body>
<p><a href="#" onclick="showConfirm(); return false;">Show Confirm</a></p>
</body>
</html>
Windows Phone 7 and 8 Quirks
-
There is no built-in browser function for
window.confirm
, but you can bind it by assigning:window.confirm = navigator.notification.confirm;
-
Calls to
alert
andconfirm
are non-blocking, so the result is only available asynchronously.