为什么js onunload在弹出页面打开时被调用? [英] Why js onunload is invoked when popup page opens?
问题描述
https://developer.mozilla.org/zh/DOM/window.onunload
当
文档被卸载时引发unload事件。
下面的代码会触发警报,即使子窗口(即变量名称win)刚刚打开,而不是关闭。
<$ p $警告(失败,但仍然重新加载:+ win.isSuccess);
失败,但仍然重载:undefined
我的注意力是调用当子窗口关闭时onunload。我在这里做错了什么?
代码
function showInsertPopup(){
var ddId = document.getElementById(workRegionDropdown);
var index = ddId.selectedIndex;
var workRegionCode = ddId.options [index] .value;
if(workRegionCode!=){
var win = window.open(machineFormPopup +?typeFlag = 1& workRegionCode =+ workRegionCode,window1,menubar = no,width = 700 ,height = 550,toolbar = no);
win.onunload = function(){
if(win.isSuccess){
alert(success reload!)
getRecordByWorkRegion();
else {
//在这里得到打印出来
alert(失败但仍然重载:+ win.isSuccess);
getRecordByWorkRegion();
}
} //结束内部函数onunload
}
} //结束showInsertPopup()
子窗口页面只有简单的js:
window.isSuccess = 1;
window.close();
首先在弹出窗口中输入$ c> about:blank 文档。 (您可以通过查看窗口的位置来验证这一点)。当弹出窗口关闭时,您应该看到另一个卸载。
According to the documentation (I'm only using Firefox) :
https://developer.mozilla.org/en/DOM/window.onunload
The unload event is raised when the document is unloaded.
But my code below would trigger the alert even though the child window (i.e. variable name "win") has just been opened, not closed.
alert("failed but still reload:" + win.isSuccess);
"failed but still reload: undefined"
My attention is to invoke that onunload when the child window is closed. What am I doing wrong here??
Code
function showInsertPopup() {
var ddId = document.getElementById("workRegionDropdown");
var index = ddId.selectedIndex;
var workRegionCode = ddId.options[index].value;
if(workRegionCode != "") {
var win = window.open(machineFormPopup + "?typeFlag=1&workRegionCode=" + workRegionCode, "window1", "menubar=no, width=700, height=550, toolbar=no");
win.onunload = function() {
if(win.isSuccess) {
alert("success reload!")
getRecordByWorkRegion();
}
else {
//here gets print out somehow
alert("failed but still reload:" + win.isSuccess);
getRecordByWorkRegion();
}
}//end inner function onunload
}
}//end showInsertPopup()
Child window page has just simple js:
window.isSuccess = 1;
window.close();
You actually see the unload for the original about:blank
document in the popup window first. (You can verify this by looking at the window's location.) You should then see another unload when the popup window closes.
这篇关于为什么js onunload在弹出页面打开时被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!