Bumpy
Bumpy

Reputation: 1332

Close window - How to determine how window was opened?

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?

Upvotes: 1

Views: 448

Answers (1)

Kernel James
Kernel James

Reputation: 4064

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>

Upvotes: 1

Related Questions