javascript:当有多个显示器时查找屏幕分辨率

发布于 2024-08-19 06:48:05 字数 806 浏览 7 评论 0原文

我使用这段代码来确定客户端屏幕分辨率,然后记录它

<input type="hidden" id="DFF63C7E-FB32-49AE-8ADA-3AB5C4834FB0" name="DFF63C7E-FB32-49AE-8ADA-3AB5C4834FB0"/>
 <input type="hidden" id="565C07CF-0D37-41DE-B47D-A247E9BD231B" name="565C07CF-0D37-41DE-B47D-A247E9BD231B" />
<script type="text/javascript" language="javascript">
         document.getElementById("DFF63C7E-FB32-49AE-8ADA-3AB5C4834FB0").value = screen.width;  
         document.getElementById("565C07CF-0D37-41DE-B47D-A247E9BD231B").value = screen.height;
</script>

在我的本地主机中测试时工作正常,但我从生产日志中发现屏幕分辨率始终为 480x640。有谁知道为什么会发生这种情况?

我做了一些更多的挖掘,发现我可以更改主显示器的分辨率,无论我是否在主/辅助显示器中打开浏览器,它总是会给我主显示器的分辨率。

有没有办法根据浏览器打开的显示器来获取屏幕分辨率?

显然,下一个问题是,如果分辨率中途发生变化(例如从控制面板)或将浏览器从一个浏览器移到另一个浏览器,是否有办法用 JS 检测/更新它?

I am using this piece of code to determine the client screen resolution, and then logging it

<input type="hidden" id="DFF63C7E-FB32-49AE-8ADA-3AB5C4834FB0" name="DFF63C7E-FB32-49AE-8ADA-3AB5C4834FB0"/>
 <input type="hidden" id="565C07CF-0D37-41DE-B47D-A247E9BD231B" name="565C07CF-0D37-41DE-B47D-A247E9BD231B" />
<script type="text/javascript" language="javascript">
         document.getElementById("DFF63C7E-FB32-49AE-8ADA-3AB5C4834FB0").value = screen.width;  
         document.getElementById("565C07CF-0D37-41DE-B47D-A247E9BD231B").value = screen.height;
</script>

It worked OK when testing in my localhost, but what I found out from the production logs was that the screen resolution was always 480x640. Does anyone know why this happens ?

And I did some more digging and found that I could change the resolution of my primary monitor and regardless of whether I open my browser in primary/secondary monitor, it would always give me the resolution of the primary monitor.

Is there any way to get the screen resolution based on which monitor the browser gets opened ?

And obviously the next question is, if the resolution changes midway (say from control panel) or moving the browser from one to another, is there a way to detect/update it with JS ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

叹梦 2024-08-26 06:48:05
  1. 您的生产 PC 可能确实具有 640*480 的屏幕分辨率。有理由怀疑吗?
  2. Javascript无法离开虚拟机来查看真实主机,因此您必须通过检测屏幕分辨率的相同方式来实现更新机制,无论它是什么是。
  1. It might be that your production PC really has 640*480 screen resolution. Is there a reason to doubt it?
  2. Javascript has no means getting out of its virtual machine to take even a glance on the real host, so you must implement your update mechanism by the same means you detect the screen resolution, whatever it is.
无法言说的痛 2024-08-26 06:48:05

将以下行添加到您的页面。

window.moveTo(0,0); window.resizeTo(screen.availWidth,screen.availHeight);" 

它将调整您的窗口大小到完整的宽度和高度..

例如使用

<body onload="window.moveTo(0,0); window.resizeTo(screen.availWidth,screen.availHeight);" >

Add the following line to your page.

window.moveTo(0,0); window.resizeTo(screen.availWidth,screen.availHeight);" 

it will resize your window to full width and height..

for example use

<body onload="window.moveTo(0,0); window.resizeTo(screen.availWidth,screen.availHeight);" >
樱花坊 2024-08-26 06:48:05

尝试以下方法来获取窗口的尺寸:

window.document.width //Get the width
window.document.height //Get the height

Try this to get the dimensions of the window instead:

window.document.width //Get the width
window.document.height //Get the height
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文