19

I am developing a web site where users can change settings which they have to confirm before taking effect.

The confirmation is done by a link I send them via E-Mail. In the HTML of the website I use this little snippet:

<script type="text/javascript">window.name="mysite";</script>

And in the HTML emails I use

<a href="..." target="mysite">Click me</a>

But Chrome is always opening new tabs instead of opening them all in one.

Is this even possible or is it forbidden for some reasons?

Simon Hessner
  • 1,757
  • 1
  • 22
  • 49
  • 1
    I tried assigning to `window.top.name` using the [browser context name](http://www.w3.org/html/wg/drafts/html/master/browsers.html#browsing-context-names) spec, but Chrome also opened a new tab when clicking on the link targeted at my existing tab. – Dan Dascalescu Jul 09 '15 at 13:38
  • 1
    Have you tried [`base`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base) element? – Vlad Zhukov Jul 09 '15 at 13:47

3 Answers3

9

Webmail platforms such as Gmail tend to modify some of the HTML code of an email due to security reasons.

They obviously remove any javascript code the email could have. But they also change (or add if none) the target property of every anchor element and set them to target="_blank" in order to avoid the user to be taken out of Gmail (in this case).

Unfortunately every webmail platform has their own behavior, therefore, what you want to do is not gonna work on every webmail platform.

If what you want to do is prevent the user from having multiple tabs of the same page opened, (*please refer to Update 1) it comes to mind you could use web sockets to close the previous tab once the user enters in the URL sent by email. Have a look at socket.io for example.

Update 1

There's no way to do this using WebSockets. There's no possible way to close a window that wasn't opened using javascript, and it can only be closed by it's parents.

Community
  • 1
  • 1
tin
  • 834
  • 6
  • 16
  • 3
    I'm interested in how you think a web socket is going to get around the rule that javascript can't close windows they didn't actually open? – Guy Schalnat Jul 10 '15 at 02:43
  • Looks like [intercepting LocalStorage events would allow the first tab to close itself when the second one stores a certain key in LocalStorage](http://stackoverflow.com/questions/28230845/communication-between-tabs-or-windows). – Dan Dascalescu Jul 11 '15 at 02:51
  • 1
    You're right @GuySchalnat there's no way to do this since the rule you said. That would be something good to try, Dan. – tin Jul 16 '15 at 13:10
8

That is a very interesting idea. I like it. Alas, it appears that, in modern browsers, you can no longer close a window you didn't open through javascript. So if you aren't allowed to run javascript in the email, the best you can do is to redirect the original page to a "thank you" page and leave it hanging around in the browser's tab (but no longer waiting on conformation). Like this:

PleaseConfirm.html:

window.name="need_redirected";

Confirm.html:

var w = window.open("", "need_redirected");
if (w) 
    w.location="ThankYou.html";

Of course, for old IE, I'd still try to close the old window in ThankYou.html:

window.top.close();

You can still try to set the target, of course, just in case it works, and you can always try putting an onclick attribute on your tag for the same reason:

<a href="confirm.html" target="need_redirected" onclick="window.open('confirm.html', 'need_redirected');">click here </a>

But that seems to be the best you can do. Bummer.

Guy Schalnat
  • 1,697
  • 15
  • 26
3

Neither of the other two answers work, but this one probably will:

  1. In the initial tab, listen for an onstorage event, with a certain key being created, e.g. "userHasConfirmedEmail". When the event occurs, window.top.close().
  2. In the new tab, create that key.

Credit goes to Tomas and his answer.

Community
  • 1
  • 1
Dan Dascalescu
  • 143,271
  • 52
  • 317
  • 404
  • 3
    Nice idea, but more recent browser versions will prevent JavaScript from closing tabs that were not first opened by that same script unfortunately. It seems we're snookered at this point with the issue raised by OP. – John Rix Dec 13 '17 at 16:15