I am trying to open a window, store a reference to it in a var and then make sure the window closes if the opening window closes, also if the button to open the window is clicked while the window is opened (and maybe behind the opening window) it will refresh the page and focus the window to pop it to the top. For now it only needs to work in IE 5.5+
I want to keep all of the script for this in one block for future encapsulation into an aspx control, so it must not rely on script inside the opened window. All of this works except for one hitch. The line that registers the onBeforeUnload event seems to work in the sense that if I do an alert(exportWindow.onBefor
eUnload) it will show me the registered function, but the function does not actually execute. I have experimented with registering different events in the opened window from the opener, but they never fire. What's the problem?
Here's the code:
var exportWindow;
function openMapExportWindow() {
if (exportWindow) {
exportWindow.location = 'Export.aspx';
exportWindow.focus();
alert(exportWindow);
} else {
var winX = window.screenTop;
var winY = window.screenLeft;
var top = winX;
var left = winY;
windowProperties = "width=400,height=400,top=
" + top + ",left=" + left + ",resizable=yes,scrollbars
=auto";
exportWindow = window.open("Export.aspx",
"ExportWindow", windowProperties);
// This seems to register, but does not execute!?
exportWindow.onBeforeUnloa
d = function() {opener.exportWindow = null;};
exportWindow.focus();
event.cancelBubble = true; // prevent the toolbar from posting back to execute server side function
}
}
closeExportWindow() {
if(exportWindow) exportWindow.close;
}
addUnloadEvent (closeMapExportWindow);
function addUnloadEvent(functionNam
e) {
var oldonunload = window.onunload;
if (typeof window.onUnload != 'function') {
window.onUnload = functionName;
} else {
window.onUnload = function() {
oldonunload();
functionName();
}
}
}
Start Free Trial