Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何仅为iframe重写windows.onbeforeunload函数?_Javascript_Event Handling_Removeeventlistener - Fatal编程技术网

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。