window.open() on a multi-monitor/dual-monitor system - where does window pop up?

前端 未结 7 786
执笔经年
执笔经年 2020-11-29 23:03

When using javascript window.open() on a multi-monitor system, how do you control which monitor, or where in the display space the popup opens? It seems out of control to me

7条回答
  •  悲&欢浪女
    2020-11-29 23:46

    FUNCTION:

    function PopupCenter(url, title, w, h, opts) {
       var _innerOpts = '';
       if(opts !== null && typeof opts === 'object' ){
           for (var p in opts ) {
               if (opts.hasOwnProperty(p)) {
                   _innerOpts += p + '=' + opts[p] + ',';
               }
           }
       }
         // Fixes dual-screen position, Most browsers, Firefox
       var dualScreenLeft = window.screenLeft != undefined ? window.screenLeft : screen.left;
       var dualScreenTop = window.screenTop != undefined ? window.screenTop : screen.top;
    
       var width = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth ? document.documentElement.clientWidth : screen.width;
       var height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight ? document.documentElement.clientHeight : screen.height;
    
       var left = ((width / 2) - (w / 2)) + dualScreenLeft;
       var top = ((height / 2) - (h / 2)) + dualScreenTop;
       var newWindow = window.open(url, title, _innerOpts + ' width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);
    
    // Puts focus on the newWindow
       if (window.focus) {
           newWindow.focus();
       }
    }
    

    USAGE:

    PopupCenter('http://www.google.com','google.com','900','500', {toolbar:1, resizable:1, location:1, menubar:1, status:1}); 
    

    It will also work on minimized windows

提交回复
热议问题