3

很简单:我想禁用/覆盖alert().

我可以这样做吗?

更重要的是,这样做是否正确?

严格模式呢?

4

2 回答 2

7

是的,您可以禁用或覆盖alert(). 不,这样做是不对的,除非在一些奇怪和有限的情况下。

禁用:

window.alert = function() { }; 

覆盖:

window.alert = function(text) { /* do something */ };
于 2010-12-03T07:14:06.950 回答
7

Yes you can, it's your choice. You could also store the original 'alert':

window.nativeAlert = window.alert;
window.alert = function(val){console.log(val+' (alert disabled)');};

now the old alert is still usable: nativeAlert('something');

于 2010-12-03T07:39:11.360 回答