JavaScript:弹出窗口立即关闭
我正在尝试使用javascript关闭所有打开的弹出窗口。我尝试了以下代码,但没有成功 这是我打开弹出窗口的代码JavaScript:弹出窗口立即关闭,javascript,Javascript,我正在尝试使用javascript关闭所有打开的弹出窗口。我尝试了以下代码,但没有成功 这是我打开弹出窗口的代码 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.o
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;
}
function CloseChildWindow()
{
if (childWindow.length != 0)
{
for (var i = 0; i < childWindow.length; i++)
{
childWindow[i].close();
}
}
}
这是我关闭所有打开的弹出窗口的代码
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;
}
function CloseChildWindow()
{
if (childWindow.length != 0)
{
for (var i = 0; i < childWindow.length; i++)
{
childWindow[i].close();
}
}
}
函数CloseChildWindow()
{
如果(childWindow.length!=0)
{
对于(变量i=0;i
我在页面的onunload事件主体中调用函数“CloseChildWindow()”。但是当我试图打开弹出窗口时,它会立即关闭,因此弹出窗口没有完全打开
我的代码有什么问题
提前感谢。弹出窗口必须由用户单击启动。我假设这对你来说不是问题,因为你会看到一个弹出窗口,哪怕只是一秒钟。这意味着浏览器没有阻止它 但是,如果在单击链接时执行此操作,则会触发
onunload
事件。。。即使页面本身没有卸载!当我在一个特别雄心勃勃的项目中工作时,这是一个主要问题
您可能需要添加一个“不卸载”标志,该标志在弹出窗口打开并在
setTimeout
后清除时设置。然后,您的onunload
事件可以简单地检查该标志,如果设置了该标志,则什么也不做。这段代码很好。你能提供一个带有一些随机弹出窗口的JSFIDLE浏览器弹出窗口拦截器吗?