简体   繁体   English

切换标签的JavaScript

[英]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. 即使您在另一个标签中,也会在该标签中打开新的URL。 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. 我认为这可行,因为当我说“ _newtab_home”时,它基本上是为该选项卡命名的,带有该目标的所有内容都将在该选项卡中打开。 Which is cool, and I understand that, but how would I make it open that tab whenever it loads the new URL? 哪个很棒,我理解这一点,但是当它加载新的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: 我希望能够为尚未访问的位置打开一个新标签,但是在您访问它们并尝试返回首页(“ http://exampleurl.com/example ”)之后,我希望它切换回已经打开过的标签页。我知道有一种方法可以使用标签页号在JavaScript中切换标签页,如下所示:

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? 这可以在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.) (如果这个问题不清楚,我非常抱歉,请随时发表评论并提出有关问题。)

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: HTML:

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

JS: 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/ 小提琴: http : //jsfiddle.net/zwnwskay/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM