用于关闭在 IE、Firefox 和 Chrome 中工作的选项卡的 Javascript 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8301780/
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
Javascript Code to close tab that works in IE, Firefox and Chrome?
提问by oneiros
Can anyone come with advice on how to close a tab in javascript that works for all browsers? Certain code snippets work only for certain browsers - anyone have a kind of a universal way that will cover the major browsers?
任何人都可以就如何关闭适用于所有浏览器的 javascript 选项卡提出建议吗?某些代码片段仅适用于某些浏览器 - 任何人都有一种覆盖主要浏览器的通用方式?
回答by Diodeus - James MacFarlane
In general, only browser windows created using JavaScript can be closed using JavaScript. Otherwise malware would be closing all of our browser windows on us.
通常,只有使用 JavaScript 创建的浏览器窗口才能使用 JavaScript 关闭。否则恶意软件会关闭我们所有的浏览器窗口。
回答by Tracker1
As already stated, you can only close windows/tabs that you created... The open in a new tab is a behavior depending on a given browser's settings.
如前所述,您只能关闭您创建的窗口/选项卡......在新选项卡中打开是一种取决于给定浏览器设置的行为。
//keep a handle to the window you open.
var newWin = window.open('my window', 'http://.../');
...
//some point later in the code
newWin.close();
回答by álvaro González
If you open a window using JavaScript, you can close it later with window.close(), which has been around since the days of Netscape Navigator. I'm not aware of any other method.
如果您使用 JavaScript 打开一个窗口,您可以稍后使用window.close()关闭它,它自 Netscape Navigator 时代就已存在。我不知道任何其他方法。
If you didn't open the window in the first place, there's no way for you to close it. It's a security mechanism that (again) has been around since the 1990s. As far as I know, there's no major browser that lacks it.
如果你一开始没有打开窗户,你就没有办法关闭它。这是一种(再次)自 1990 年代以来一直存在的安全机制。据我所知,没有主流浏览器缺少它。
I can't figure out what kind of snippets you've found out there (maybe some Flash or VBScript?) but there isn't really much more to say about the subject.
我不知道你在那里发现了什么样的片段(也许是一些 Flash 或 VBScript?)但关于这个主题真的没有更多要说的。