Javascript 多监视器/双监视器系统上的window.open()-窗口在哪里弹出?

Javascript 多监视器/双监视器系统上的window.open()-窗口在哪里弹出?,javascript,popup,window,window.open,multiple-monitors,Javascript,Popup,Window,Window.open,Multiple Monitors,在多监视器系统上使用javascript window.open()时,如何控制哪个监视器或弹出窗口在显示空间中的何处打开?在我看来,它似乎失去了控制,而且它的行为也是随机的。对“window.open dual screen”搜索的结果显示了这个奇特的金块: “当用户单击一个链接,该链接使用 window.open。使窗口显示在与其相同的监视器上 家长。” 如果它成功地允许您在启动它的文档的同一屏幕上打开一个弹出窗口,那么通过类似的努力,您应该能够修改它,使其表现出不同的行为 请注意,正如代码

在多监视器系统上使用javascript window.open()时,如何控制哪个监视器或弹出窗口在显示空间中的何处打开?在我看来,它似乎失去了控制,而且它的行为也是随机的。

对“window.open dual screen”搜索的结果显示了这个奇特的金块:

“当用户单击一个链接,该链接使用 window.open。使窗口显示在与其相同的监视器上 家长。”

如果它成功地允许您在启动它的文档的同一屏幕上打开一个弹出窗口,那么通过类似的努力,您应该能够修改它,使其表现出不同的行为

请注意,正如代码长度所暗示的,jquery/javascript/浏览器中没有内置函数来理解多个监视器,只是双屏桌面只是一个放大的单笛卡尔平面,而不是两个离散平面

更新

链接已断开。使用“window.open dual screen”搜索的结果显示了这个奇特的金块:

“当用户单击一个链接,该链接使用 window.open。使窗口显示在与其相同的监视器上 家长。”

如果它成功地允许您在启动它的文档的同一屏幕上打开一个弹出窗口,那么通过类似的努力,您应该能够修改它,使其表现出不同的行为

请注意,正如代码长度所暗示的,jquery/javascript/浏览器中没有内置函数来理解多个监视器,只是双屏桌面只是一个放大的单笛卡尔平面,而不是两个离散平面

更新


链接已断开。使用

窗口。screenX
将给出当前监视器屏幕的位置

假设监视器宽度为1360

对于监视器1
window.screenX=0

对于监视器2
window.screenX=1360

因此,通过使用
window.screenX
添加左侧位置,弹出窗口将在预期位置打开

function openWindow() {

    var width = 650;
    var left = 200;

    left += window.screenX;

    window.open(thePage,'windowName','resizable=1,scrollbars=1,fullscreen=0,height=200,width=' + width + '  , left=' + left + ', toolbar=0, menubar=0,status=1');    
    return 0;

}

窗口。screenX
将给出当前监视器屏幕的位置

假设监视器宽度为1360

对于监视器1
window.screenX=0

对于监视器2
window.screenX=1360

因此,通过使用
window.screenX
添加左侧位置,弹出窗口将在预期位置打开

function openWindow() {

    var width = 650;
    var left = 200;

    left += window.screenX;

    window.open(thePage,'windowName','resizable=1,scrollbars=1,fullscreen=0,height=200,width=' + width + '  , left=' + left + ', toolbar=0, menubar=0,status=1');    
    return 0;

}
功能:

用法:

它还可用于最小化窗口功能:

用法:


它也适用于最小化的窗口

上述解决方案都不能正常工作。在精确中心方面,

我试过这个,它在chrome和firefox中对我很好

var sY = screenY;
        if (sY < 0) {
            sY = 0;
        }
        var totalScreenWidth = (screenX + window.outerWidth + sY);
        if (totalScreenWidth > screen.width) {
            totalScreenWidth = totalScreenWidth / 2;
        } else {
            totalScreenWidth = 0;
        }
        windowobj.moveTo(totalScreenWidth + ((screen.width - h) / 2), ((screen.height - h) / 2));
var sY=screenY;
if(sY<0){
sY=0;
}
var totalScreenWidth=(screenX+window.outerWidth+sY);
如果(总屏幕宽度>屏幕宽度){
totalScreenWidth=totalScreenWidth/2;
}否则{
总屏幕宽度=0;
}
windowobj.moveTo(总屏幕宽度+((screen.width-h)/2),((screen.height-h)/2);

但是,如果第二个监视器浏览器在第一个屏幕上显示一半,在第二个屏幕上显示另一半,这也会产生问题。

上述解决方案都无法正常工作。在精确中心方面,

我试过这个,它在chrome和firefox中对我很好

var sY = screenY;
        if (sY < 0) {
            sY = 0;
        }
        var totalScreenWidth = (screenX + window.outerWidth + sY);
        if (totalScreenWidth > screen.width) {
            totalScreenWidth = totalScreenWidth / 2;
        } else {
            totalScreenWidth = 0;
        }
        windowobj.moveTo(totalScreenWidth + ((screen.width - h) / 2), ((screen.height - h) / 2));
var sY=screenY;
if(sY<0){
sY=0;
}
var totalScreenWidth=(screenX+window.outerWidth+sY);
如果(总屏幕宽度>屏幕宽度){
totalScreenWidth=totalScreenWidth/2;
}否则{
总屏幕宽度=0;
}
windowobj.moveTo(总屏幕宽度+((screen.width-h)/2),((screen.height-h)/2);

但是,如果第二个监视器浏览器的一半在第一个监视器浏览器中查看,另一半在第二个监视器浏览器中查看,这也会产生问题。

基于javascript的解决方案由于安全原因无法工作

我有另一个想法给你,为什么不使用铬扩展处理定位。(那里没有安全问题) 当然,它只适用于chrome(也许这对你很好)

背景:我们遇到了相关的困难。在windows中打开多个文档并需要放置在其他监视器中的内部webapp。 出于安全原因,javascript不支持这一点,并且只有本机扩展才能正确处理选项卡/窗口对象

因此,我们创建了一个开源的chrome扩展来实现这一点:跨多监视器设置灵活的windows位置

在您的情况下,您可以很容易地为每个监视器定义一个规则,该规则在何处以及如何显示。您可以在chrome扩展的选项页面中执行此操作。 (安装后,您可以直接前往那里)

铬合金扩展称为“多窗口定位器”,其完全免费。你可以在chrome商店买到

您在项目的github中找到的实际源代码

免责声明:我是开源(MIT)github项目的维护者。如果有任何有趣的想法或评论,请随时分享

更新2020年9月21日我离开那家公司时,不再是开源项目的维护者。也就是说,我不理解否决票,浏览器没有提供window.api,允许您清楚地理解多个监视器,因为这是一个明确的安全限制/违反。这就是浏览器插件扩展的帮助所在,因为它们不是一个网页,它们可以访问额外的API,以便准确地查找/列出监视器和窗口。它们遵循不同的安全上下文,因此具有额外的权限。并非所有浏览器都提供require extension.api以支持mutlmonitor。Mozilla Firefox基本上没有。Chrome和最新MS Edge是。 经过这么多年的经验,我仍然100%相信没有简单的javascript解决方案,唯一的方法是一个涉及浏览的解决方案
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();
   }
}
PopupCenter('http://www.google.com','google.com','900','500', {toolbar:1, resizable:1, location:1, menubar:1, status:1}); 
var sY = screenY;
        if (sY < 0) {
            sY = 0;
        }
        var totalScreenWidth = (screenX + window.outerWidth + sY);
        if (totalScreenWidth > screen.width) {
            totalScreenWidth = totalScreenWidth / 2;
        } else {
            totalScreenWidth = 0;
        }
        windowobj.moveTo(totalScreenWidth + ((screen.width - h) / 2), ((screen.height - h) / 2));
function openWindow( url, winnm, options)
{
    var wLeft = parseInt( typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft );

    var left = 0;

    if( (result = /left=(\d+)/g.exec(options)) ) {
        left = parseInt(result[1]);
    }

    if(options)
    {
       options = options.replace("left="+left,"left="+(parseInt(left) + wLeft));        
       w = window.open( url, winnm, options );
    }
    else
    {
       w = window.open( url, winnm );
    }

    if(w)
         w.focus();

    return w;
}