js 屏蔽 alert() confirm() prompt() 后的恢复


alert 弹窗太麻烦,js 一句代码就屏蔽。

屏蔽一时爽,恢复火葬场

alert = null;

alert = console.log;

我要恢复使用alert弹窗呢?

没得恢复。

alert是window对象的一个方法,将alert赋值为其他的后,原alert就没有引用了,就找不到了。

恢复思路:保存alert方法的引用!

于是就有了网上的代码:

但是,通过阅读W3School的文章,window对象总会初始化,在新加载一个iframe时。

灵感来了,新建一个iframe,复制引用iframe中的window对象的alert函数。

image

$("iframe").contentWindow,找到iframe的window对象。

alert(1),尝试alert一下。

window.alert = console.log,禁用 alert。

alert(1),再尝试alert,已经变成控制台输出了。

$("iframe").contentWindow.alert,找到iframe里面的alert。

window.alert = $("iframe").contentWindow.alert,复制引用,恢复window对象的alert方法引用

alert(1),再alert一次,已经恢复了。

image

如图


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM