Close window - How to determine how window was opened?

点点圈 提交于 2019-12-31 04:39:06

问题


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

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

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

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

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.

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


回答1:


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

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

Then:

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


来源:https://stackoverflow.com/questions/17689005/close-window-how-to-determine-how-window-was-opened

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!