Videos
What is the difference between alert() and confirm() in JavaScript?
Is the confirm() method supported in all web browsers?
Can I use the confirm() method without attaching it to an event?
The trouble here is that you can't pause execution in javascript, so you'll need to adjust your confirm function (which you should probably rename, by the way, since JS has a native confirm function). Get rid of the onclick of your yes button, and adjust your confirm function like so:
function confirm(message,nobutton,yesbutton,yesfunction){
$('#overlaymessage').html(message);
$('#nobutton').html(nobutton);
$('#yesbutton').html(yesbutton);
$('#overlay').show();
$('#yesbutton').off("click").click(yesfunction);
}
Then, pass a function into your confirm call:
function deleteuser(userid){
function deleteConfirmed() {
// delete code here
}
confirm('Delete user?','No','Yes',deleteConfirmed);
}
You need to capture the result from your confirm dialogue box e.g.
var x = confirm("Are you sure you are ok?");
if (x) {
alert("Good!");
} else {
alert("Too bad");
}
alert, confirm, and prompt are all DOM APIs that are implemented by the browser. They do not create DOM elements to represent them, and their functionality cannot be exactly recreated with JavaScript because you cannot force the JavaScript engine to wait for the user to click one of your buttons. You can only come close by requiring a callback that will contain the result of the dialog that you create.
function customConfirm(message, callback) {
message = message.replace(/"\n"/g, "<br />")
createElement();
//The create Element is very complicated to create the text box including message. . .
function clickOkay() {
callback(true);
}
function clickCancel() {
callback(false);
}
}
customConfirm("Hello World!", function (result) {
console.log(result);
});
The confirm()-function is a native function brought to you by every browser.
It is not created by using javascript. However if you want to reproduce that behaviour you go a way similar to this:
function myConfirm(text, cb){
// We need this later
var body = document.getElementsByTagName('body')[0];
// First we create a div which holds the alert and give it a class to style it with css
var overlay = document.createElement('div');
overlay.className = 'myConfirm';
// The box holds the content
var box = document.createElement('div');
var p = document.createElement('p');
p.appendChild(document.createTextNode(text));
// We append the text to the div
box.appendChild(p);
// Create yes and no button
var yesButton = document.createElement('button');
var noButton = document.createElement('button');
// Add text and events to the buttons
yesButton.appendChild(document.createTextNode('Yes'));
yesButton.addEventListener('click', function(){ cb(true); body.removeChild(overlay); }, false);
noButton.appendChild(document.createTextNode('No'));
noButton.addEventListener('click', function(){ cb(false); body.removeChild(overlay); }, false);
// Append the buttons to the box
box.appendChild(yesButton);
box.appendChild(noButton);
// Append the box to the Overlay
overlay.appendChild(box)
// insert the Overlay with box into the dom
body.appendChild(overlay);
}
myConfirm('Hello there!', function(value){ console.log(value); });
.myConfirm{
position:fixed;
width:100%;
height:100%;
background:rgba(0,0,0,0.05);
}
.myConfirm>div{
width:200px;
margin:10% auto;
padding:10px 20px;
border:1px solid black;
background:#ccc;
}
.myConfirm div p{
text-align:center;
}
.myConfirm div button{
width:40%;
margin:0 5%;
}
This could be an implementation for a self-made alert-box.
Note that the native alert is a synchronous-function. That means the browser stops the JavasScript-engine until the alert-box is closed. You cant clone that behavior but at least you can give the function a callback which is called, when one of the buttons is clicked. In this case the callback just logs the value to the console.
Hope I could help here!
UPDATE to the updated question
Back in the days when JavaScript was new confirm was a valid way to ask the user for a specific value. confirm, prompt and alert are special functions from these days which behave completely different than normal functions since they break the JavaScript-"flow".
When you ask why: Well - maybe it was a nice to have-feature back in these days. Note that in earlier versions alert looked like a system-message (in IE it still does). At least in Firefox you can nowadays interact normally with your browser even if alert is called.
That's why it is merely used for debugging only today (and even here console.log is the better choice).
I am pretty sure that today (in FF) the alert is rendered with browser-intern html and CSS, too.