Disable a built-in function in javascript (alert)

空扰寡人 提交于 2019-12-18 05:16:11

问题


Simple: I want to disable/overwrite alert().

Can I do this?

More importantly, is it right to do this?

What about strict mode?


回答1:


Yes, you can disable or overwrite alert(). No, it's not right to do it, except in some bizarre and limited situations.

Disable:

window.alert = function() { }; 

Override:

window.alert = function(text) { /* do something */ };



回答2:


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');



来源:https://stackoverflow.com/questions/4343255/disable-a-built-in-function-in-javascript-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!