检测多个打开的选项卡...并关闭最新标签! (通过BroadcastChannel API)
我已经将此功能放在负载上,因此我可以检测一个新的选项卡:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决两个选项卡的解决方案。有关更多选项卡,需要一些主 /从索引。
Solution for 2 tabs. For more tabs need some master / slave index.