如何让JavaScript在当前监视器上打开弹出窗口

时间:2008-09-11 21:06:22

标签: javascript multiple-monitors

情景:

  1. 用户有两台显示器。
  2. 他们的浏览器在辅助显示器上打开。
  3. 他们点击浏览器中的一个链接,该链接调用带有特定顶部和左侧窗口偏移的window.open()。
  4. 弹出窗口始终在主监视器上打开。
  5. 在JavaScript中是否有任何方法可以让弹出窗口在与初始浏览器窗口(开启者)相同的监视器上打开?

8 个答案:

答案 0 :(得分:22)

您无法指定监视器,但可以将弹出窗口的位置指定为相对于单击导致窗口弹出的位置。

使用getMouseXY()函数获取要作为window.open()方法的左箭头和顶箭头传递的值。 (左侧和顶部的args仅适用于V3和更高版本的浏览器)。

window.open docs: http://www.javascripter.net/faq/openinga.htm

function getMouseXY( e ) {
    if ( event.clientX ) { // Grab the x-y pos.s if browser is IE.
        CurrentLeft = event.clientX + document.body.scrollLeft;
        CurrentTop  = event.clientY + document.body.scrollTop;
    }
    else {  // Grab the x-y pos.s if browser isn't IE.
        CurrentLeft = e.pageX;
        CurrentTop  = e.pageY;
    }  
    if ( CurrentLeft < 0 ) { CurrentLeft = 0; };
    if ( CurrentTop  < 0 ) { CurrentTop  = 0; };  

    return true;
}

答案 1 :(得分:18)

这是我从Facebook oauth API无耻地逆向设计的东西。在Firefox / Chrome中的主显示器和辅助显示器上进行了测试。

function popup_params(width, height) {
    var a = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft;
    var i = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop;
    var g = typeof window.outerWidth!='undefined' ? window.outerWidth : document.documentElement.clientWidth;
    var f = typeof window.outerHeight != 'undefined' ? window.outerHeight: (document.documentElement.clientHeight - 22);
    var h = (a < 0) ? window.screen.width + a : a;
    var left = parseInt(h + ((g - width) / 2), 10);
    var top = parseInt(i + ((f-height) / 2.5), 10);
    return 'width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',scrollbars=1';
}   

window.open(url, "window name", "location=1,toolbar=0," + popup_params(modal_width, modal_height));

答案 2 :(得分:6)

// Pops a window relative to the current window position
function popup(url, winName, xOffset, yOffset) {
  var x = (window.screenX || window.screenLeft || 0) + (xOffset || 0);
  var y = (window.screenY || window.screenTop || 0) + (yOffset || 0);
  return window.open(url, winName, 'top=' +y+ ',left=' +x))
}

如下所示调用它,它将在当前窗口的顶部打开

popup('http://www.google.com', 'my-win');

或略微偏移

popup('http://www.google.com', 'my-win', 30, 30);

重点是window.screenX / screenLeft为您提供与整个桌面相关的位置,而不仅仅是显示器。

window.screen.left将是您提供所需信息的理想候选人。问题是它在页面加载时设置,用户可以将窗口移动到另一个监视器。

更多研究

此问题的最终解决方案(不仅仅是偏离当前窗口位置)需要知道窗口所在屏幕的尺寸。由于屏幕对象不会随着用户移动窗口而更新,我们需要制作我们自己的检测当前屏幕分辨率的方法。这是我想出来的

/**
 * Finds the screen element for the monitor that the browser window is currently in.
 * This is required because window.screen is the screen that the page was originally
 * loaded in. This method works even after the window has been moved across monitors.
 * 
 * @param {function} cb The function that will be called (asynchronously) once the screen 
 * object has been discovered. It will be passed a single argument, the screen object.
 */
function getScreenProps (cb) {
    if (!window.frames.testiframe) {
      var iframeEl = document.createElement('iframe');
      iframeEl.name = 'testiframe';
      iframeEl.src = "about:blank";
      iframeEl.id = 'iframe-test'
      document.body.appendChild(iframeEl);
    }

    // Callback when the iframe finishes reloading, it will have the 
    // correct screen object
    document.getElementById('iframe-test').onload = function() {
      cb( window.frames.testiframe.screen );
      delete document.getElementById('iframe-test').onload;
    };
    // reload the iframe so that the screen object is reloaded
    window.frames.testiframe.location.reload();
};

因此,如果您想要始终打开窗口所在监视器左上角的窗口,您可以使用以下内容:

function openAtTopLeftOfSameMonitor(url, winName) {
  getScreenProps(function(scr){
    window.open(url, winName, 'top=0,left=' + scr.left);
  })
}

答案 3 :(得分:2)

在当前显示器上打开居中窗口,同时使用Chrome:

function popupOnCurrentScreenCenter(url, title, w, h) {
    var dualScreenLeft = typeof window.screenLeft !== "undefined" ? window.screenLeft : screen.left;
    var dualScreenTop = typeof 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, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

    // Puts focus on the newWindow
    if (window.focus) {
        newWindow.focus();
    }
}

答案 4 :(得分:0)

如果您知道每台显示器的分辨率,您可以估算一下。 对于公共网站来说这是一个坏主意,但如果你知道(出于某种奇怪的原因)这个场景将永远适用,那么它可能会很有用。

鼠标的相对位置(如上所述)或原始浏览器窗口也可能有用,但您必须假设用户使用最大化的浏览器(不一定是这样)。

答案 5 :(得分:0)

我最近遇到了这个问题,终于找到了一种方法来将弹出窗口定位在触发它的屏幕上。在我的github页面上查看我的解决方案:https://github.com/svignara/windowPopUp

诀窍在于使用window.screen对象,该对象返回availWidthavailHeightavailLeftavailTop值(以及widthheight)。有关对象中变量的完整列表以及这些变量代表的内容,请查看https://developer.mozilla.org/en-US/docs/DOM/window.screen

基本上,只要单击弹出窗口的触发器,我的解决方案就会找到window.screen的值。这样我就可以确定从哪个监视器屏幕点击它。 availLeft值负责其余部分。方法如下:

  
    

基本上,如果左边的第一个可用像素(availLeft)是负数,那就告诉我们“主”监视器左侧有一个监视器。同样,如果左边的第一个可用像素大于0,则表示以下两种情况之一:

         
        
  1. 显示器位于“主”显示器右侧,或
  2.     
  3. 屏幕左侧有一些“垃圾”(可能是应用程序底座或Windows开始菜单)
  4.        

在任何一种情况下,您都希望弹出窗口的偏移量从左侧的可用像素开始。

offsetLeft = availableLeft + ( (availableWidth - modalWidth) / 2 )

答案 6 :(得分:0)

仅user11153的版本适用于Chrome和双屏。这是它的TypeScript版本。

popupOnCurrentScreenCenter(url: string, title: string, w: number, h: number): Window|null {
    var dualScreenLeft = typeof window.screenLeft !== "undefined" ? window.screenLeft : (<any>screen).left;
    var dualScreenTop = typeof window.screenTop !== "undefined" ? window.screenTop : (<any>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, 'scrollbars=yes, width=' + w + ', height=' + h + ', top=' + top + ', left=' + left);

    // Puts focus on the newWindow
    if (window.focus && newWindow) {
        newWindow.focus();
    }
    return newWindow;
}

答案 7 :(得分:-2)

只要你知道你可以做的特定监视器上的x和y位置:

var x = 0;
var y = 0;
var myWin = window.open(''+self.location,'mywin','left='+x+',top='+y+',width=500,height=500,toolbar=1,resizable=0');