Is there a way to override alert("")
and confirm("")
in javascript?
I use jQuery, so if there is a way to do it in this framework, I'd appreciate it.
I'd like to do stuff like:
override alert(msg){
//Show custom stuff here
}
override confirm(msg){
//Show custom stuff here.
return watever;
}
Yes and no. You can simply replace them:
window.alert = function(msg){
// stuff
}
but you're not going to get the blocking functionality that alert and confirm give you, i.e. there will be NO way to get this code to work:
if(confirm('Do Something?')){
// do stuff
}
You're generally better off making other functions to do similar things than you are trying to replace these.
Just redefine it:
window.alert = function(msg) { console.log(msg); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With