关闭其他标签页或浏览器的脚本

18

我正在为销售开发的MVC应用程序中有一个按钮,该按钮打开一个不允许在新标签页中打开iframe的网页。因此,销售代理使用此按钮时,经常不关闭应用程序打开的选项卡,最终一天结束时可能会有20-30个打开的选项卡。因此,我想知道是否有一种脚本可以添加到新按钮中,可以关闭:

  1. 要么关闭包含所有选项卡的整个浏览器,以便他们可以重新开始;或者
  2. 关闭所有其他选项卡,而不影响当前选项卡。

在视图中,我有:

HTML

<input type="submit" onclick="return OpenInNewTab('http://test.com');" name="command" value="nonIframe" background-image:url(../images/URL/Test.png);" class="submit" />

Javascript

//Function OpenInNewTab
function OpenInNewTab(url) {
    var win = window.open(url, '_blank');
    win.focus();
    return false;
}

我正在玩这个脚本,但它只关闭当前标签页。我想要保持当前标签页打开并且关闭所有其他标签页或者关闭整个浏览器。

Javascript

<script language="JavaScript">
    function closeIt() {
        close();
    }
</script>

HTML

<center>
    <form>
        <input type=button value="Close Window" onClick="closeIt()">
    </form>
</center>

非常感谢您的任何建议。 谢谢


1
你的OpenInNewTab()函数的来源是什么? 你可以通过保存window.open()的返回值来跟踪你打开的窗口(标签页)。 - Stephen P
@StephenP,感谢您指出这一点。我已经更新了它。 - user2908229
1
另一个需要考虑的问题是使用窗口名称而不是 _blank,例如 var win = window.open(url, 'workTab'); — 这样它会重复使用相同的标签页而不是每次打开一个新的。如果销售人员实际上需要同时打开多个选项卡,则可能无法接受此方法。最后一个提示是学习如何从单独的 .js 文件中附加事件处理程序,而不是使用内联的 onclick=... 处理程序。 - Stephen P
这要看情况,开启按钮在哪里?是每个页面都有还是只有根页面有? - Ayyash
4个回答

25

只有拥有窗口句柄才能关闭窗口。也就是说,您的页面需要是打开它们的页面(或者您以某种方式传递了句柄)。

例如:

var winGoogle = window.open('http://google.com', '_blank');
var winBing = window.open('http://bing.com', '_blank');
var winYahoo = window.open('http://yahoo.com', '_blank');

//close the windows
winGoogle.close();
winBing.close();
winYahoo.close();
你可以将这些新窗口存储在一个数组中,在需要关闭它们的时候遍历这些句柄。
你可以把这些新的窗口存储在一个数组中,当需要关闭它们时,可以遍历这些句柄。
var windows = [];
//each time you open a new window, simply add it like this:
windows.push(window.open('http://google.com', '_blank'));

//then you can iterate over them and close them all like this:
for(var i = 0; i < windows.length; i++){
    windows[i].close()
}

3
好的,我会尽力进行翻译。根据您的要求,以下是翻译的内容:没错,当我发表评论时,我正在考虑使用push()和iterate。 +1 - Stephen P
仍然,它正在执行函数但没有关闭任何东西。让我再试一次。同时,有人知道我们是否可以将Ctrl+F4组合复制到按钮上以便关闭吗?不确定是否有人已经尝试过?我在考虑尝试这样做。如果(evt.altKey && evt.keyCode==115)的话,则条件成立。 - user2908229
@user2908229 不确定为什么它对你不起作用。你不能告诉你的用户,他们可以右键单击一个选项卡并选择“关闭其他选项卡”或“关闭右侧选项卡”吗?你能像Stephen P建议的那样每次覆盖相同的选项卡吗? - Gray
1
我会对此进行轻微调整,以跟踪所有打开的窗口,通过将其推送到最父级窗口集合中...而不是使用windows.push,您可以尝试查找window.top.windows,并循环直到没有顶级窗口,然后将其添加到集合中。现在关闭按钮也应该尝试查找最父级窗口集合,诀窍是排除触发事件的窗口... - Ayyash

3
HTML:
<a href="javascript: window.open('', '_self', '').close();">Close Tab</a>

JavaScript:

window.open('', '_self', '').close();

IT会在不提示权限的情况下关闭当前标签页。


最新版的Chrome会给你一个静默处理。 - FerdousTheWebCoder

0

基于@Gray的答案的工作原型:

    <html lang="en">
    <head>
        <title>Sample-close specific tabs</title>
    </head>
    <body style="text-align: center;">
            <a href="#" onClick="openGoogle();">Google</a> | 
            <a href="#" onClick="openBing();">Bing</a> | 
            <a href="#" onClick="openYahoo();">Yahoo</a><br />
            <a href="#" onClick="closeAll()">closeAll</a><br />
    </body>
        <script>

            var winGoogle;
            var winBing;
            var winYahoo;

            var windows = [];

            function openGoogle(){
                winGoogle = window.open('https://google.com', '_blank');
                windows.push(winGoogle);
            }

            function openBing(){
                winBing = window.open('https://bing.com', '_blank');
                windows.push(winBing);
            }

            function openYahoo(){
                winYahoo = window.open('https://yahoo.com', '_blank');
                windows.push(winYahoo);
            }

            function closeAll(){
                console.log("length: " + windows.length);

                for(var i = 0; i < windows.length; i++){
                    windows[i].close()
                    console.log(windows[0]);
                }
            }
        </script>
    </html>

0

在Mohamed Musthaque的出色回答基础上...我想打开一个新标签页并关闭当前标签页。这在Chrome中完美实现。

<a href='https:...'
  onclick='window.open(this.href); window.open("", "_self", "").close(); return false;'> 
  Open new tab, close this one
</a>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接