5

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 ?

Jeyaprakash
  • 151
  • 3
  • 11

2 Answers2

2

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
Nishad K Ahamed
  • 1,374
  • 15
  • 25
1

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

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • Thank you so much it works fine in chrome browser. But do not work in Other browser like (Firefox). Is Any solution for this? – Jeyaprakash Jan 05 '16 at 08:51
  • 1
    I afraid that it's not possible because of security reason on `Firefox`. http://stackoverflow.com/questions/22052388/how-to-get-window-focus-to-work-in-firefox – Mosh Feu Jan 05 '16 at 09:50