Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get the newly opened browser tab name from origin tab?

My code like this to create the new tab from one tab to another in same browser.

function newTab()
{
    var form1 = document.createElement("form");
    form1.id = "newTab1"
    form1.method = "GET";
    form1.action = "domainname";  //My site address
    form1.target = "framename";   //Browser tab name
    document.body.appendChild(form1);
    form1.submit();
}

Above code works correctly to create the new tab . when click from "MyNewTab" link the home page

<a href="javascript:newTab()" style=" margin-left:15px;" id="newTab"> MyNewTab </a>

Again i try to switch over home page to newly opened tab. This time need to switch from home page to new Tab without reload. But this newtab content reloaded.

How to get the tab name and get focus the newly opened tab from click the "MyNewTab" link ?

like image 448
Jeyaprakash Avatar asked Oct 18 '22 19:10

Jeyaprakash


2 Answers

you need to set the current selected tab in javascript cookie, and always read this cookie at page load and set the tab

you can set the cookie inside the newTab() function:

function newTab()
{
    var form1 = document.createElement("form");
    form1.id = "newTab1"
    form1.method = "GET";
    form1.action = "domainname";  //My site address
    form1.target = "framename";   //Browser tab name

    $.cookie("currentTab", "framename");//set the selected tab name in cookie

    document.body.appendChild(form1);
    form1.submit();
}

then, on page load event:

var tabName=$.cookie("currentTab")//get the current tab name from cookie.
if(tabName==null){
tabName="default tab name"//at first time the cookie will be null, 
//so you need to assign a default value if the cookie is null
}

//set this tab as selected
like image 160
Nishad K Ahamed Avatar answered Oct 21 '22 16:10

Nishad K Ahamed


If I understand you correctly, you just need to use window.open() and window.focus().

Explanation: When user click on the button you check if the new tab already open. If so, you just use window.focus(). If not, you open it using window.open().

Like this:

var new_win;
document.querySelector('button').onclick = function(){
  if (new_win) {
    new_win.focus();
  }
  else {
    new_win = window.open('second_page_url');
  }
};
<button>Open tab</button>

Note: This code not working in the snippet (because the security) so you can see the it in http://jsbin.com/tebecu

like image 20
Mosh Feu Avatar answered Oct 21 '22 14:10

Mosh Feu