Use window.name to open links in emails in the same tab

3.8k views Asked by At

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?

3

There are 3 answers

0
Guy Schalnat On BEST ANSWER

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.

3
tin On

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.

1
Dan Dascalescu On

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.