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

IT技术 javascript html
2021-03-19 04:24:51

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

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

当我移动到另一个页面并再次返回该页面时,弹出窗口会重新打开,尽管它已经打开了。请指导我正确的方向,以便如果弹出窗口已经打开,则不应再次打开。我试过了,document.referred但它需要网站在线,目前我正在离线工作。

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

为窗口命名。像这样将名称基于您的域,可以防止您选择其他人碰巧选择的名称。

永远不要组成以 开头的名称_,这些名称是为浏览器以不同方式处理的特殊名称保留的(与锚元素的“目标”属性相同)。

请注意,如果该名称的窗口使用不同的选项(例如不同的高度)打开,那么它将保留这些选项。此处的选项仅在没有该名称的窗口时才会生效,因此您需要创建一个新窗口。

编辑:

请注意,“名称”是窗口的,而不是内容的。它不会影响标题(newWindow.document.title会影响它,当然会在 中编码abc.html)。它确实会影响其他跨窗口执行操作的尝试。因此,另一个window.open具有相同名称的窗口将重用此窗口。还有一个像这样的链接<a href="def.html" target="com_MyDomain_myWindowForThisPurpose">clicky!</a>会重复使用它。关于浏览器在各种情况下拒绝打开窗口(弹出窗口阻止)的正常警告适用。

@SaghirA.Khatri 真的,你发现这是在什么浏览器中发生的?
2021-05-01 04:24:51
它基本上是一个命名标题的指令,它将重新打开窗口。
2021-05-03 04:24:51
值得注意的是,它与标题无关,我将编辑以添加注释。
2021-05-11 04:24:51
这实际上是我的错,我没有全神贯注地注意到这一点,谢谢,希望你不要介意。:)
2021-05-15 04:24:51
NP。我只是很困惑它不起作用,javascript 总是可以有“除了在这些浏览器中......”但是这在第一个 javascript 浏览器 NN2 中起作用,所以我想也许一些新的弹出窗口阻止正在干扰。
2021-05-20 04:24:51

打开一个窗口并在页面刷新之间保持对它的引用。

var winref = window.open('', 'MyWindowName', '', true);
if(winref.location.href === 'about:blank'){
    winref.location.href = 'http://example.com';
}

或以函数格式

function openOnce(url, target){
    // open a blank "target" window
    // or get the reference to the existing "target" window
    var winref = window.open('', target, '', true);

    // if the "target" window was just opened, change its url
    if(winref.location.href === 'about:blank'){
        winref.location.href = url;
    }
    return winref;
}
openOnce('http://example.com', 'MyWindowName');
@DanielM 我认为它是特定于浏览器的,参数是这些:URL、名称、规格、替换geeksforgeeks.org/javascript-window-open-window-close-method
2021-05-10 04:24:51
window.open() 只有 3 个参数
2021-05-15 04:24:51
这很有趣... W3Schools 还列出了 4 个参数。然而,MDN 和 HTML 规范都没有(甚至没有被弃用),所以我不知道其他人使用什么作为第四个参数的来源。无论如何,这不是标准功能,不应使用。
2021-05-17 04:24:51

您可以通过在关闭时重新分配对窗口的引用来检查窗口是打开还是关闭。例子:

var newWindow;
var openWindow = function(){
    newWindow = newWindow || window.open('newpage.html');
    newWindow.focus();
    newWindow.onbeforeunload = function(){
        newWindow = null;
    };
};

使用“关闭”属性:如果窗口已关闭,则其关闭属性将为真。 https://developer.mozilla.org/en-US/docs/Web/API/Window/closed

当您移动到另一个页面(在同一域上)时,您可以使用弹出页面重新设置window.open 变量,如下所示:

https://jsfiddle.net/u5w9v4gf/

尝试步骤:

  1. 单击运行(在 jsfiddle 编辑器上)。
  2. 单击“试用我”(预览时)。
  3. 单击运行移动到另一个页面,变量将被重新设置。

代码 :

window.currentChild = false;

$("#tryme").click(function() {
    if (currentChild) currentChild.close();
    const child = window.open("about:blank", "lmao", 'width=250,height=300');
    currentChild = child;

    //Scrope script in child windows
    child.frames.eval(`
    setInterval(function () {
        if (!window.opener.currentChild)
            window.opener.currentChild = window;
    }, 500);
  `);
});


setInterval(function() {
console.log(currentChild)
    if (!currentChild || (currentChild && currentChild.closed))
        $("p").text("No popup/child. :(")
    else
        $("p").text("Child detected !")
}, 500);