关闭窗口 - 如何确定窗口是如何打开的? [英] Close window - How to determine how window was opened?

查看:24
本文介绍了关闭窗口 - 如何确定窗口是如何打开的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个页面上,我有一个带有 target="_blank" 的超链接.

On one page, I have a hyperlink with target="_blank".

在目标页面上,我有一个关闭"按钮,它使用 JS 关闭新选项卡/窗口.

On the target page, I have a "Close" button which uses JS to close the new tab/window.

  1. 如果用户点击超链接,我可以使用JS关闭打开的窗口.太棒了!

  1. If the user clicks the hyperlink, I can use JS to close the opened window. Great!

如果用户右键单击链接并选择在新选项卡中打开",我的 JS 事件无法关闭窗口.嘘.:-(

If the user right-clicks the link and selects "Open in new tab", my JS event can not close the window. Boo. :-(

我理解这样做的原因,但是在第二种情况下,如果窗口无法通过 Javascript 关闭,我只想不显示关闭"按钮.

I understand the reasons for this, however in the second case I'd like to just not show the "Close" button if the window won't be able to be closed by Javascript.

所以我的问题是:如何预先确定窗口是否可以使用 Javascript 关闭?

So my question: How do I pre-determine whether the window is closeable with Javascript?

推荐答案

不要使用 target="_blank",使用 target="popup1".然后在你的新标签中检测它:

Don't use target="_blank", use target="popup1". Then in your new tab detect it with:

if(window.name == "popup1") {
  document.body.className += " closeable";
}

那么:

<style>
.closeable #closebutton {display:block}
</style>

这篇关于关闭窗口 - 如何确定窗口是如何打开的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆