Javascript 关闭其他选项卡或浏览器的脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29220735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:00:34  来源:igfitidea点击:

Script to close other tabs or browser

javascript

提问by user2908229

In my MVC application being developed for sales, I have a button which opens a web page which doesn't allow iframe to open in a new tab. So, when the sales agent use this button, they often don't close the tabs opened by the application and in the end of the day, there is 20-30 of open tabs. So, I was wondering if there is a script which I can add to a new button which could close:

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

  1. Either the complete browser with all tabs in it so they can start fresh or
  2. Close all other tabs without affecting the current tab.
  1. 包含所有选项卡的完整浏览器,以便他们可以重新开始或
  2. 关闭所有其他选项卡而不影响当前选项卡。

In the view, I have

在视图中,我有

HTML

HTML

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

Javascript

Javascript

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

I was playing with this script, but it only closes the current tab. I want the current tab to be open and close all other tabs or close the entire browser itself.

我正在玩这个脚本,但它只关闭当前选项卡。我希望当前选项卡打开并关闭所有其他选项卡或关闭整个浏览器本身。

Javascript

Javascript

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

HTML

HTML

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

Any suggestions would be really appreciated. Thank You

任何建议将不胜感激。谢谢你

回答by Gray

You can only close windows if you have their handle. That means your page needs to have been the one that opened them (or you somehow passed the handle around).

如果你有他们的句柄,你只能关闭窗口。这意味着您的页面必须是打开它们的页面(或者您以某种方式传递了句柄)。

Example:

例子:

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();

You could store these new windows in an array, and iterate over the handles when it is time to close them.

您可以将这些新窗口存储在一个数组中,并在需要关闭它们时遍历这些句柄。

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()
}

回答by Mohamed Musthaque

HTML:

HTML:

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

javascript:

javascript:

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

IT will close the current tab without prompting a permission.

IT 将在不提示许可的情况下关闭当前选项卡。

回答by George Fisher

Building on Mohamed Musthaque's excellent answer ... I wanted to open a new tab and close the current one. This does it perfectly in Chrome

以 Mohamed Musthaque 的出色回答为基础......我想打开一个新标签并关闭当前标签。这在 Chrome 中做得很完美

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