Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

js window.confirm

// window.confirm(message);
/*
`window.confirm` opens a confirmation dialog with an "Ok"
and "Cancel" button. Upon receiving input, the dialog is
closed and a boolean is returned. `window.confirm` returns
true if the "Ok" button was pressed or false if "Cancel"
was pressed.
*/

const process_something = window.confirm("Are you sure you would like to process something?");
if (process_something) {
	window.alert("Processing something...");
} else {
	window.alert("Operation cancelled.");
}
/*
Note that this will pause code execution until
the dialog receives input. You can't fully get
around this, however using asynchronous
functions or promises, you can get other
statements to be called just after the dialog
is closed and before the dialog returns its
response.
*/
window.confirm = (function() {
	const synchronous_confirm = window.confirm;
	return async function(message) {
		return synchronous_confirm(message);
	};
})();
// OR
window.confirm = (function() {
	const synchronous_confirm = window.confirm;
	return function(message) {
		return new Promise((res, rej) => {
			try {
				res(synchronous_confirm(message));
			} catch (error) {
				rej(error);
			}
		});
	};
})();
 
PREVIOUS NEXT
Tagged: #js
ADD COMMENT
Topic
Name
9+5 =