Javascript: find screen resolution with multiple monitors

I use this code snippet to determine the client screen resolution and then register 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 fine when testing in my local host, but what I learned from production logs was that the screen resolution was always 480x640. Does anyone know why this is happening?

And I did a few more digging and found that I can change the resolution of my primary monitor and regardless of whether I open my browser on the primary / secondary monitor, it always gives me the resolution of the primary monitor.

Is there a way to get screen resolution based on which monitor the browser opens?

And, obviously, the next question: if the resolution changes halfway (say, from the control panel) or moving the browser from one to the other, is there any way to detect / update it using JS?

+3
source share
3 answers
  • Perhaps your production PC really has a screen resolution of 640 * 480. Is there any reason to doubt that?
  • Javascript does not have the ability to exit its virtual machine to look at the real host, so you must implement your update mechanism by the same means as you determine the screen resolution, whatever that is.
0
source

Try this to get the window dimensions:

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

.

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

.

for example use

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

Source: https://habr.com/ru/post/1729933/


All Articles