How to open maximized window with Javascript?

前端 未结 5 1390
独厮守ぢ
独厮守ぢ 2020-11-28 07:45

I\'m using Javascript\'s self.open() to open a link in a new window and i\'d like that window to be maximized. I tried the fullscreen=yes option, w

相关标签:
5条回答
  • 2020-11-28 08:23

    The best solution I could find at present time to open a window maximized is (Internet Explorer 11, Chrome 49, Firefox 45):

      var popup = window.open("your_url", "popup", "fullscreen");
      if (popup.outerWidth < screen.availWidth || popup.outerHeight < screen.availHeight)
      {
        popup.moveTo(0,0);
        popup.resizeTo(screen.availWidth, screen.availHeight);
      }
    

    see https://jsfiddle.net/8xwocrp6/7/

    Note 1: It does not work on Edge (13.1058686). Not sure whether it's a bug or if it's as designed (I've filled a bug report, we'll see what they have to say about it). Here is a workaround:

    if (navigator.userAgent.match(/Edge\/\d+/g))
    {
        return window.open("your_url", "popup", "width=" + screen.width + ",height=" + screen.height);
    }
    

    Note 2: moveTo or resizeTo will not work (Access denied) if the window you are opening is on another domain.

    0 讨论(0)
  • 2020-11-28 08:25
    var params = [
        'height='+screen.height,
        'width='+screen.width,
        'fullscreen=yes' // only works in IE, but here for completeness
    ].join(',');
         // and any other options from
         // https://developer.mozilla.org/en/DOM/window.open
    
    var popup = window.open('http://www.google.com', 'popup_window', params); 
    popup.moveTo(0,0);
    

    Please refrain from opening the popup unless the user really wants it, otherwise they will curse you and blacklist your site. ;-)

    edit: Oops, as Joren Van Severen points out in a comment, this may not take into account taskbars and window decorations (in a possibly browser-dependent way). Be aware. It seems that ignoring height and width (only param is fullscreen=yes) seems to work on Chrome and perhaps Firefox too; the original 'fullscreen' functionality has been disabled in Firefox for being obnoxious, but has been replaced with maximization. This directly contradicts information on the same page of https://developer.mozilla.org/en/DOM/window.open which says that window-maximizing is impossible. This 'feature' may or may not be supported depending on the browser.

    0 讨论(0)
  • 2020-11-28 08:33
     window.open('your_url', 'popup_name','height=' + screen.height + ',width=' + screen.width + ',resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes')
    
    0 讨论(0)
  • 2020-11-28 08:40

    If I use Firefox then screen.width and screen.height works fine but in IE and Chrome they don't work properly instead it opens with the minimum size.

    And yes I tried giving too large numbers too like 10000 for both height and width but not exactly the maximized effect.

    0 讨论(0)
  • 2020-11-28 08:44

    Checkout this jquery window plugin: http://fstoke.me/jquery/window/

    // create a window
    sampleWnd = $.window({
       .....
    });
    
    // resize the window by passed w,h parameter
    sampleWnd.resize(screen.width, screen.height);
    
    0 讨论(0)
提交回复
热议问题