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

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

屏蔽一时爽,恢复火葬场对象

alert = null;blog

alert = console.log;get

我要恢复使用alert弹窗呢?iframe

没得恢复。it

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

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

因而就有了网上的代码:方法

var temp = alert; alert=null; alert(1); alert=temp;

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

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

image

$("iframe").contentWindow alert(1) window.alert = console.log alert(1) $("iframe").contentWindow.alert window.alert = $("iframe").contentWindow.alert alert(1)

$("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

如图