覆盖警报并在Javascript中确认

有没有办法在javascript中覆盖alert("")confirm("")

我使用jQuery,所以如果有一种方法可以在这个框架中完成它,我会很感激。

我想做的事情如下:

 override alert(msg){ //Show custom stuff here } override confirm(msg){ //Show custom stuff here. return watever; } 

是的,不是。 你可以简单地替换它们:

 window.alert = function(msg){ // stuff } 

但是你不会得到警告和确认给你的阻止function,即没有办法让这段代码工作:

 if(confirm('Do Something?')){ // do stuff } 

你通常会做其他function来做类似的事情,而不是试图替换它们。

重新定义它:

 window.alert = function(msg) { console.log(msg); } 

只需定义一个名为alertconfirm的函数。

 function alert(){}; function confirm(){}; 

但是,如果需要引用原始函数,则应通过分配var oldAlert或仅在另一个函数内定义alert来保存对这些函数的引用,因此window.alert保持不变。

 (function() { function alert(){}; // alert inside of this scope is your custom function })();