`window.open`

问题描述 投票:0回答:6
当我使用 Firefox,然后使用

window.open('blah.com','blah','left=-30,top=-300');

 时,弹出窗口会在第一个显示器上方的第二个显示器中打开,但在 Chrome 中,弹出窗口仅在 
left=0,top=0
 处打开。 chrome 这样做有什么原因吗?我该如何解决这个问题?

谢谢!

javascript html google-chrome popup window.open
6个回答
25
投票
这是 Chrome 在辅助显示器上打开弹出窗口时的一个错误。 Chrome 人员似乎说这是一个安全问题(尽管我不明白这是一个安全问题)。

现在,在不存在的辅助显示器上打开弹出窗口,我可以理解这是一个安全问题,但是......无论如何。

这是有关此事的讨论的链接:

https://code.google.com/p/chromium/issues/detail?id=137681


18
投票
我知道这是一篇旧文章,但这是我的解决方案。只需使用屏幕对象的“avail*”属性即可:

var windowSize = { width: 500, height: 500, }; var windowLocation = { left: (window.screen.availLeft + (window.screen.availWidth / 2)) - (windowSize.width / 2), top: (window.screen.availTop + (window.screen.availHeight / 2)) - (windowSize.height / 2) }; window.open(http://example.com, '_blank', 'width=' + windowSize.width + ', height=' + windowSize.height + ', left=' + windowLocation.left + ', top=' + windowLocation.top);

基本上,“window.screen.availLeft”为您提供其他屏幕宽度,以便您可以将正常的中心计算添加到其中。


11
投票
var w = 300; var h = 300; var left = (window.screen.width/2)-(w/2); var top = (window.screen.height/2)-(h/2); var win = window.open("example.html", "_blank", 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h); win.moveTo(left, top);

适用于

Chrome...


1
投票
看看这个 StackOverflow 的帖子

在屏幕上居中弹出窗口?

几年前我对该功能进行了一些升级。

function multipleScreenPopup(url, title, w, h, centered = true, moveRight = 0, moveDown = 0, resizable = "no") { // 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 = 0; var top = 0; if (centered === true) { left = ((width / 2) - (w / 2)) + dualScreenLeft; top = ((height / 2) - (h / 2)) + dualScreenTop; } else { left = dualScreenLeft + moveRight; top = dualScreenTop + moveDown; } var newWindow = window.open(url, title, 'directories=no,titlebar=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=' + resizable + ', width=' + w + ', height=' + h + ', top=' + top + ', left=' + left); // Puts focus on the newWindow if (window.focus) { newWindow.focus(); } } // centered as of the monitor that fired on it multipleScreenPopup('https://google.com', '_blank', 500, 500); // move from the left and from the top multipleScreenPopup('https://yahoo.com', '_blank', 500, 500, false, 200, 200);
    

0
投票
我知道这篇文章很旧,但我也遇到过类似的问题:

var w = 300; var h = 300; var left = (window.screen.width/2)-(w/2); var top = (window.screen.height/2)-(h/2); var win = window.open("example.html", "_blank", 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);

-- 在 Firefox 中将弹出窗口居中,但
不在 Chrome 中。另外,请注意,这不在显示区域之外...


0
投票
这是可行的,尽管该方法是实验性的,将来可能不起作用

var screenDetails = await window.getScreenDetails() if (screen.isExtended && screenDetails.screens.length > 1) { var newWindow = window.open( "https://ya.ru", "New Window", `popup,width=${myWidth},height=${myHeight},left=0,top=0` ); newWindow.moveTo(screenDetails.screens[1].left + myMargin, 0); }
    
© www.soinside.com 2019 - 2024. All rights reserved.