JavaScript open in a new window, not tab

前端 未结 15 1280
余生分开走
余生分开走 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:44

    I just tried this with IE (11) and Chrome (54.0.2794.1 canary SyzyASan):

    window.open(url, "_blank", "x=y")
    

    ... and it opened in a new window.

    Which means that Clint pachl had it right when he said that providing any one parameter will cause the new window to open.

    -- and apparently it doesn't have to be a legitimate parameter!

    (YMMV - as I said, I only tested it in two places...and the next upgrade might invalidate the results, any way)

    ETA: I just noticed, though - in IE, the window has no decorations.

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

    I had this same question but found a relatively simple solution to it.

    In JavaScript I was checking for window.opener !=null; to determine if the window was a pop up. If you're using some similar detection code to determine if the window you're site is being rendered in is a pop up you can easily "turn it off" when you want to open a "new" window using the new windows JavaScript.

    Just put this at the top of your page you want to always be a "new" window.

    <script type="text/javascript">
        window.opener=null;
    </script>
    

    I use this on the log in page of my site so users don't get pop up behavior if they use a pop up window to navigate to my site.

    You could even create a simple redirect page that does this and then moves to the URL you gave it. Something like,

    JavaScript on parent page:

    window.open("MyRedirect.html?URL="+URL, "_blank");
    

    And then by using a little javascript from here you can get the URL and redirect to it.

    JavaScript on Redirect Page:

     <script type="text/javascript">
            window.opener=null;
    
        function getSearchParameters() {
              var prmstr = window.location.search.substr(1);
              return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
        }
    
        function transformToAssocArray( prmstr ) {
            var params = {};
            var prmarr = prmstr.split("&");
            for ( var i = 0; i < prmarr.length; i++) {
                var tmparr = prmarr[i].split("=");
                params[tmparr[0]] = tmparr[1];
            }
            return params;
        }
    
        var params = getSearchParameters();
        window.location = params.URL;
        </script>
    
    0 讨论(0)
  • 2020-11-21 22:45

    Try:

    window.open("", [window name], "height=XXX,width=XXX,modal=yes,alwaysRaised=yes");
    

    I have some code that does what your say, but there is a lot of parameters in it. I think these are the bare minimum, let me know if it doesn't work, I'll post the rest.

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

    Interestingly, I found that if you pass in an empty string (as opposed to a null string, or a list of properties) for the third attribute of window.open, it would open in a new tab for Chrome, Firefox, and IE. If absent, the behavior was different.

    So, this is my new call:

     window.open(url, windowName, '');
    
    0 讨论(0)
  • 2020-11-21 22:49

    I may be wrong, but from what I understand, this is controlled by the user's browser preferences, and I do not believe that this can be overridden.

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

    try that method.....

    function popitup(url) {
           //alert(url);
           newwindow=window.open("http://www.zeeshanakhter.com","_blank","toolbar=yes,scrollbars=yes, resizable=yes, top=500, left=500, width=400, height=400");
           newwindow.moveTo(350,150);
       if (window.focus) 
              {
                 newwindow.focus()
              }
       return false;
      }
    
    0 讨论(0)
提交回复
热议问题