user5166359
user5166359

Reputation:

Open a link in new tab, open another link and reload on that tab

I'm not sure if there's a proper term for this behavior.

User story (there are 2 tabs):
1. User clicks first link on Tab1, a new tab (Tab2) opens up
2. User clicks second link on Tab1, Tab2 refreshes to reflect second link clicked by user on Tab1.

How can I achieve this scenario?

Upvotes: 0

Views: 2072

Answers (2)

Minato
Minato

Reputation: 4533

Just adding to @Hafiz's answer

You can open a "named tab" in Javascript using window.open() call.

For example:

/* The code I am writing is extremely stupid and should never be used professionally, it's just here to convey the point */
/* you html element. */
<a href="#" onlick="openLink('google.com','tab2', true)">1st Link</a>
<a href="#" onlick="openLink('google.de','tab2', false)">2nd Link</a>

/* openLink */
function openLink(url, tabName, focus) {
   var tab = window.open(url, tabName);
   if(focus)
      /* additionally if you want to focus on the tab, this might not work on some platform. Safari(may be on iOS) I think. */
      tab.focus(); 
}

You can read up on open() and focus().

Upvotes: 1

hafiz
hafiz

Reputation: 151

You can open the first link in a new tab using a handle:

var tab = window.open('http://yoursite.com/action', '_blank');

and then simply change the location of the opened window when the user clicks the second link:

tab.location.href = 'http://yoursite.com/second-action';

Upvotes: 1

Related Questions