Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 317
Simon Hessner Avatar asked Jun 11 '15 17:06

Simon Hessner


People also ask

How do I make a hyperlink open in the same tab?

Use _self in target attribute of anchor tag to Open link in same tab in HTML webpage.

How do I open a link in a specific window?

Open in a new window To open a link in a new browser window, hold the Shift on then click the link or right-click the link and select Open link in New Window.

How do I make links open in a new window by default?

How to Open Hyperlinks in a New Browser Tab or Window. The short answer is: just add a target="_blank" attribute to your links (anchor tags). Now when your visitors click that link, it will open in a new window or tab (depending on which web browser they are using and how they configured that browser).

How do I get links to open in the same window in Chrome?

For that link the only way to open in the same window is via drag 'n' drop the link into the same window or else use the context menu to open the link in the same window.


Video Answer


3 Answers

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.

like image 63
Guy Schalnat Avatar answered Oct 21 '22 15:10

Guy Schalnat


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.

like image 26
tin Avatar answered Oct 21 '22 14:10

tin


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.

like image 43
Dan Dascalescu Avatar answered Oct 21 '22 16:10

Dan Dascalescu