检查窗口是否已经打开window.open

检查窗口是否已经打开window.open

问题描述:

我有一个html页面。在页面的主体中,我调用 onload 事件,它调用javascript函数打开弹出窗口。这里是代码:

I have a html page. In the body of the page I am calling onload event which calls javascript function to open a pop up window. here is the code:

var newWindow = null;
function launchApplication()
{
    if ((newWindow == null) || (newWindow.closed))
    {
        newWindow = window.open('abc.html','','height=960px,width=940px');
    }
}

当我移到另一个页面时,该页面再次弹出重新打开,虽然它已被打开。请指导我正确的方向,这样如果弹出窗口已经打开,那么它不应该再次打开。我尝试了 document.referred 但它需要网站在线,目前我正在离线工作。

when I move to another page, and come back to that page again, popup reopens, although it is already opened. Please guide me to proper direction so that if pop up is already open then it should not open again. I tried document.referred but it requires the site online, currently I am working offline.

newWindow = window.open('abc.html','com_MyDomain_myWindowForThisPurpose','height=960px,width=940px');

为窗口命名。以这种名义在您的域名上,防止您挑选别人偶然选择的名字的机会。

Give the window a name. Basing the name on your domain like this, prevents the chances of you picking a name someone else happened to choose.

永远不要组成一个以

Never make up a name that begins with _, those are reserved for special names the browser treats differently (same as with the "target" attribute of anchor elements).

这些保留给特殊名称的浏览器以不同的方式处理(与锚元素的target属性相同)。请注意,如果该名称的窗口以不同的选项打开(例如不同的高度),则会保留这些选项。这里的选项只有在没有该名称的窗口时才会生效,因此您可以创建一个新的名称。

Note that if the window of that name was opened with different options (e.g. different height), then it'll keep those options. The options here will only take effect if there is no window of that name, so you do create a new one.

编辑:

请注意,名称是窗口的内容,而不是内容。它不会影响标题( newWindow.document.title 会影响该标题,当然会在 abc.html )。它确实会影响其他尝试在Windows上执行任何操作。因此,具有相同名称的另一个 window.open 将重新使用此窗口。此外,像< a href =def.htmltarget =com_MyDomain_myWindowForThisPurpose> clicky!< / a> 等链接将重新使用它。关于浏览器在各种情况下阻止窗口打开的正常警告(popup-blocking)适用。

Note that the "name" is of the window, not of the content. It doesn't affect the title (newWindow.document.title will affect that, as of course will code in abc.html). It does affect other attempts to do stuff across windows. Hence another window.open with the same name will reuse this window. Also a link like <a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a> will re-use it. Normal caveats about browsers resisting window-opening in various scenarios (popup-blocking) apply.