JavaScript open in a new window, not tab

前端 未结 15 1301
余生分开走
余生分开走 2020-11-21 22:18

I have a select box that calls window.open(url) when an item is selected. Firefox will open the page in a new tab by default. However, I would like the page t

相关标签:
15条回答
  • 2020-11-21 22:55

    I think its not html target properties problem but you unchecked "open nw windows in a new tab instead" option in "tab" tab under firefox "options" menu. check it and try again.

    enter image description here

    0 讨论(0)
  • 2020-11-21 22:57

    OK, after making a lot of test, here my concluson:

    When you perform:

         window.open('www.yourdomain.tld','_blank');
         window.open('www.yourdomain.tld','myWindow');
    

    or whatever you put in the destination field, this will change nothing: the new page will be opened in a new tab (so depend on user preference)

    If you want the page to be opened in a new "real" window, you must put extra parameter. Like:

    window.open('www.yourdomain.tld', 'mywindow','location=1,status=1,scrollbars=1, resizable=1, directories=1, toolbar=1, titlebar=1');
    

    After testing, it seems the extra parameter you use, dont' really matter: this is not the fact you put "this parameter" or "this other one" which create the new "real window" but the fact there is new parameter(s).

    But something is confused and may explain a lot of wrong answers:

    This:

     win1 = window.open('myurl1', 'ID_WIN');
     win2 = window.open('myurl2', 'ID_WIN', 'location=1,status=1,scrollbars=1');
    

    And this:

     win2 = window.open('myurl2', 'ID_WIN', 'location=1,status=1,scrollbars=1');
     win1 = window.open('myurl1', 'ID_WIN');
    

    will NOT give the same result.

    In the first case, as you first open a page without extra parameter, it will open in a new tab. And in this case, the second call will be also opened in this tab because of the name you give.

    In second case, as your first call is made with extra parameter, the page will be opened in a new "real window". And in that case, even if the second call is made without the extra parameter, it will also be opened in this new "real window"... but same tab!

    This mean the first call is important as it decided where to put the page.

    0 讨论(0)
  • 2020-11-21 23:01

    The key is the parameters :

    If you provide Parameters [ Height="" , Width="" ] , then it will open in new windows.

    If you DON'T provide Parameters , then it will open in new tab.

    Tested in Chrome and Firefox

    0 讨论(0)
  • 2020-11-21 23:02

    You shouldn't need to. Allow the user to have whatever preferences they want.

    Firefox does that by default because opening a page in a new window is annoying and a page should never be allowed to do so if that is not what is desired by the user. (Firefox does allow you to open tabs in a new window if you set it that way).

    0 讨论(0)
  • 2020-11-21 23:05

    Answered here. But posting it again for reference.

    window.open() will not open in new tab if it is not happening on actual click event. In the example given the url is being opened on actual click event. This will work provided user has appropriate settings in the browser.

    <a class="link">Link</a>
    <script  type="text/javascript">
         $("a.link").on("click",function(){
             window.open('www.yourdomain.com','_blank');
         });
    </script>
    

    Similarly, if you are trying to do an ajax call within the click function and want to open a window on success, ensure you are doing the ajax call with async : false option set.

    0 讨论(0)
  • 2020-11-21 23:08

    You might try following function:

    <script type="text/javascript">
    function open(url)
    {
      var popup = window.open(url, "_blank", "width=200, height=200") ;
      popup.location = URL;
    }
    </script>
    

    The HTML code for execution:

    <a href="#" onclick="open('http://www.google.com')">google search</a>
    
    0 讨论(0)
提交回复
热议问题