I am trying to close all open pop-up using javascript. I tried the following code but it didn't work.
This is my code for opening the pop-up window..
var childWindow = new Array();
function PopupCenter(pageURL, title, w, h)
{
var left = (screen.width/2) - (w/2);
var top = (screen.height/2) - (h/2);
var targetWin = window.open(pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
childWindow[childWindow.length] = targetWin;
if (window.focus) {
targetWin.focus()
}
return false;
}
And this is my code for closing all open pop-up window..
function CloseChildWindow()
{
if (childWindow.length != 0)
{
for (var i = 0; i < childWindow.length; i++)
{
childWindow[i].close();
}
}
}
I call the function 'CloseChildWindow()' in body onunload event of the page. But when I try to open the pop-up window it closes immediately so the pop-up window didn't open completely.
What is the problem with my code?
Thanks in advance.
Pop-ups must be initiated by a user click. I'm assuming this isn't a problem for you, because you are seeing a popup, if only for a second. This means the browser isn't blocking it.
However, if you are doing this on click of a link, then the onunload
event will fire... even if the page itself isn't unloaded! I had this as a major problem when working on a particularly ambitious project.
What you may need to do is add a "don't unload" flag, which gets set when a popup is opened and cleared after a setTimeout
. Then, your onunload
event can simply check that flag and do nothing if it is set.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.