检测多个打开的选项卡...并关闭最新标签! (通过BroadcastChannel API)

发布于 2025-01-28 19:03:05 字数 720 浏览 4 评论 0原文

我已经将此功能放在负载上,因此我可以检测一个新的选项卡:

function checkNewTabs() {
  const channel = new BroadcastChannel('tab');
  channel.postMessage('newTab');
  channel.addEventListener('message', (msg) => {
    if (msg.data === 'newTab') {
      alert('Please don't open more than one tab at a time!');
      //window.open('', '_self', '').close();
    }
  });
}

我希望能够将消息发送回新选项卡:

channel.postMessage('callbackNewTab');
channel.addEventListener('message', (msgBack) => {
  if (msgBack.data === 'callbackNewTab') {
    alert('This tab will be closed!');
    window.open('', '_self', '').close();
  }
});

为了关闭新标签...

我可以为旧标签,但我真正需要的是关闭新标签!

有人可以帮我吗? tks! :)

I've put this function on load so I can detect a new tab being opened:

function checkNewTabs() {
  const channel = new BroadcastChannel('tab');
  channel.postMessage('newTab');
  channel.addEventListener('message', (msg) => {
    if (msg.data === 'newTab') {
      alert('Please don't open more than one tab at a time!');
      //window.open('', '_self', '').close();
    }
  });
}

I want to be able to send a message back to the new tab like this:

channel.postMessage('callbackNewTab');
channel.addEventListener('message', (msgBack) => {
  if (msgBack.data === 'callbackNewTab') {
    alert('This tab will be closed!');
    window.open('', '_self', '').close();
  }
});

In order to close the new tab...

I can do it for the old tab but what I really need is for it to close the new tab!

Can anyone help me out please? tks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉宸 2025-02-04 19:03:05

解决两个选项卡的解决方案。有关更多选项卡,需要一些主 /从索引。

const EVENT_NEW_TAB = "newTabOpened";
const EVENT_CLOSE_TAB = "needCloseThisTab";

const CHANNEL_NAME = "tab";

const channel = new BroadcastChannel(CHANNEL_NAME);
channel.onmessage = (event) =>
{
    let data = event.data;

    switch (data)
    {
        case EVENT_NEW_TAB:
            onNewTabOpened();
            break;
        case EVENT_CLOSE_TAB:
            onNeedCloseThisTab();
            break;
    }
}

function onNewTabOpened()
{
    channel.postMessage(EVENT_CLOSE_TAB);  // Send message to second tab
}

function onNeedCloseThisTab()
{
    alert('This tab will be closed!');

    window.open("about:blank", "_self").close(); // Need some hack
}

function onPageLoad()
{
    channel.postMessage(EVENT_NEW_TAB); // Send message to first tab
}

window.onload = () =>
{
    onPageLoad();
};

Solution for 2 tabs. For more tabs need some master / slave index.

const EVENT_NEW_TAB = "newTabOpened";
const EVENT_CLOSE_TAB = "needCloseThisTab";

const CHANNEL_NAME = "tab";

const channel = new BroadcastChannel(CHANNEL_NAME);
channel.onmessage = (event) =>
{
    let data = event.data;

    switch (data)
    {
        case EVENT_NEW_TAB:
            onNewTabOpened();
            break;
        case EVENT_CLOSE_TAB:
            onNeedCloseThisTab();
            break;
    }
}

function onNewTabOpened()
{
    channel.postMessage(EVENT_CLOSE_TAB);  // Send message to second tab
}

function onNeedCloseThisTab()
{
    alert('This tab will be closed!');

    window.open("about:blank", "_self").close(); // Need some hack
}

function onPageLoad()
{
    channel.postMessage(EVENT_NEW_TAB); // Send message to first tab
}

window.onload = () =>
{
    onPageLoad();
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文