如何在window全局作用域下既可以覆盖alert自定义提示消息,又可以再次调用window原始alert函数呢?方法就是使用框架frame(iframe)。通过在当前document中增加一个iframe,那么在任何时候都可以调用iframe中window的原始alert函数。如下代码
//window作用域下覆盖alert的方法
(function(){
var subWin = top!=window?top:null; //如果存在父框架
if(subWin==null){
var iframe = document.createElement("iframe");
iframe.style.display = 'none';
document.appendChild(iframe);//定义iframe添加进document
subWin = iframe.contentWindow;
}
window.alert = function(msg){
subWin.alert('系统提示:'+msg);//使用iframe的原始alert函数
}
})();
alert('user is null');//'系统提示:user is null'