Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override alert and confirm in Javascript

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;
}
like image 745
Shawn Mclean Avatar asked Apr 06 '11 00:04

Shawn Mclean


2 Answers

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.

like image 183
Nobody Avatar answered Sep 19 '22 14:09

Nobody


Just redefine it:

window.alert = function(msg) { console.log(msg); }
like image 29
ctcherry Avatar answered Sep 20 '22 14:09

ctcherry