Javascript 如何仅为iframe重写windows.onbeforeunload函数?
我有一个听众Javascript 如何仅为iframe重写windows.onbeforeunload函数?,javascript,event-handling,removeeventlistener,Javascript,Event Handling,Removeeventlistener,我有一个听众 window.onbeforeunload=function(evt){ //causes pop up } 我需要用iframe的另一个事件监听器覆盖它(这里下载案例)。下面的代码不起作用 var tIFrame = document.getElementById('Download'); iFrame.contentWindow.addEventListener('beforeunload', wrappedFn, false); iFrame.addEventList
window.onbeforeunload=function(evt){
//causes pop up
}
我需要用iframe的另一个事件监听器覆盖它(这里下载案例)。下面的代码不起作用
var tIFrame = document.getElementById('Download');
iFrame.contentWindow.addEventListener('beforeunload', wrappedFn, false);
iFrame.addEventListener('beforeunload',wrappedFn,true) 谢谢@Bergi的提示。下面的代码对我有用
var iFrame=document.getElementById('Download');
iFrame.contentWindow.onbeforeunload=function(){}
iFrame.contentWindow.location.href=sCmd;
您需要通过重新分配
contentWindow.onbeforeunload
来覆盖它。使用addEventListener
只添加第二个侦听器。@Bergi var unloadfunc=function(){}iFrame.contentWindow.onbeforeunload=unloadfunc;我也用这种方法试过。它仍然调用window.onbeforeunload函数。您确定在iframe内部的赋值之后发生赋值吗?@Bergi是的,我可以在debug env中看到,它正在被赋值。但它仍然在执行location.href=cmd时调用wndow.onbeforeunload。