Possible Duplicate:
How to create TRULY modal alerts/confirms in Javascript?
TL;DR: I've overridden the default alert()
function with a custom HTML based one. I want the new dialogue to still block execution, and get the buttons within my dialogue to return true
or false
from the call to alert()
to use in logic (and continue execution).
I'm trying to implement a custom alert box, which replaces the default browser alert with a nicely themed box with the same (or similar) functionality.
I've read this question, and I'm using the solution given in this answer (to the same question). What I want to do now is get my overridden alert to return a true or false value for use in if()
statements, depending on whether OK
or Cancel
was clicked:
if(alert('Confirm?') {
// Do stuff
}
However, due to having custom HTML instead of a normal alert I can't do this for two reasons:
- I can't return a value from the buttons in the replacement dialogue (click events bound with
$.on()
) because I have no idea how to. - I can't block program flow with this alert, as far as I know.
I've bound $.on()
events to the Cancel
and OK
buttons in the replacement dialogue which hide the box. These work fine, but the problem I have now is returning a value when a button is clicked, so that execution will halt until an action is taken by the user.
HTML:
<div class="alert background"></div>
<div class="alert box">
<div class="message"></div>
<hr>
<div class="buttons">
<input type="button" name="cancel" value="Cancel">
<input type="button" name="confirm" value="OK">
</div>
</div>
Current JavaScript: (pretty much a carbon copy of the answer in my linked question)
(function () {
nalert = window.alert;
Type = {
native: 'native',
custom: 'custom'
};
})();
(function (proxy) {
proxy.alert = function () {
var message = (!arguments[0]) ? 'null' : arguments[0];
var type = (!arguments[1]) ? '' : arguments[1];
if (type && type == 'native') {
nalert(message);
} else {
// Custom alert box code
console.log(message);
}
};
})(this);
Ideally, I want to be able to put something like this in the // Custom alert box code
part:
$('.alert.box input[name="confirm"]').on('click', function() {
// Hide dialogue box - I can do this already
// *** Return `true` or other truthy value from
// alert for use in `if()` statements
});
So that when the OK
or Cancel
button is clicked, it removes the custom alert box and returns a true or false value from the call to alert()
. I can already remove the alert with $.fadeOut()
and $.remove()
, that's easy. What isn't is knowing how to get the button click events to get alert()
(overridden) to return something.
I've tried to be as clear as I can, but I may have missed something out. Please let me know if I have.