Cole Z.
Cole Z.

Reputation: 83

Switching tabs JavaScript

So this is rather tough to explain, but I'll try my best. I have added event listeners on my webpage that will bring you to certain pages with the press of a key, and they work just fine. I used this code:

if(e.keyCode === 65)
    window.location.replace('http://exampleurl.com/example');

And this works just fine. I tried opening them up with new tabs like this:

if(e.keyCode === 65)
    window.open('http://exampleurl.com/example', '_newtab');

And this is also perfectly fine. Now I think it would definitely get annoying to open a new tab every time you try to visit one of the pages. I would like to make it to where if you already have one page open in one tab, pressing the key to visit that page will simply switch to the tab where it is open in. The closest I was able to get to this was by doing this:

if(e.keyCode === 65)
    window.open('http://exampleurl.com/example', '_newtab_home');

if(e.keyCode === 66)
    window.open('http://anotherexampleurl.com/example2', '_newtab_home');

And this will open up the new URL in that same tab even if you're in another tab. But it won't switch to that tab. I think this works because when I say "_newtab_home" it basically names that tab and everything with that target will open in that tab. Which is cool, and I understand that, but how would I make it open that tab whenever it loads the new URL? I want to be able to open a new tab for locations that haven't been visited, but after you visit them and try to go back to the homepage ("http://exampleurl.com/example"), I want it to switch back to the tab that it was already open in. I know there's a way to switch tabs in JavaScript using the tab numbers, like this:

var tabnum = 2;
$('ul.quicktabs_tabs li.tab' + tabnum + ' a').trigger('keyup');

But I would rather do this without tab numbers as the numbers would always be different depending on which order the user clicks them in, or whether or not they have any tabs open. I don't really know how I would do this though.

I think there are about six different locations you can visit on my site with key presses. I want each location to have its own tab to open in and I want to switch back to that tab whenever you press the key that opens it again. Is this possible to do in JavScript? If so, please help me out! Thanks in advance. (I'm very sorry if this question is unclear, feel free to comment and ask questions about it.)

Upvotes: 1

Views: 3143

Answers (1)

juvian
juvian

Reputation: 16068

Didn´t find a way to check if window is already opened, but there is a way of checking if the tab that opens the windows has already opened them storing the references of the tabs you open:

HTML:

<input type='button' id='button' value='Open' >

JS:

window.onload=function(){


    function launchApplication(l_url, l_windowName)
    {
      if ( typeof launchApplication.winrefs == 'undefined' )
      {
        launchApplication.winrefs = {};
      }
      if ( typeof launchApplication.winrefs[l_windowName] == 'undefined' || launchApplication.winrefs[l_windowName].closed )
      {

        launchApplication.winrefs[l_windowName] = window.open(l_url, l_windowName);
      } else {
        launchApplication.winrefs[l_windowName].focus()
      }
    }

    document.getElementById('button').addEventListener('click', function(){
        launchApplication('http://www.google.com', 'g')
    });

}

Fiddle: http://jsfiddle.net/zwnwskay/1/

Upvotes: 2

Related Questions