In JavaScript, I can get the screen's height with window.screen.height
. Now, if I have dual monitors, and the two monitors are different sizes, and I move the browser window from one monitor to the other, is there any way to detect that, beyond polling the window size at intervals in time?
There's no reliable way to do this, screen.height
will always return the height of the primary monitor, doesn't matter how many monitors you have, so that's not going to help at all.
Using screenX/Y doesn't really help either, as there are no indications of when one screen ends and another begins, and trying to calculate it would be hard as there's no guarantee the user starts out with the browser at the top left in one of the screens, or that the browser takes up the entire screen, and you can't get the dimensions for any other monitor than the primary screen, so there's no way to know the size of the other screens.
In other words, it's going to be hard to do this, if not impossible, and any solution will probably not be very reliable or portable, so you're better of just sticking with the browser window and detecting changes to that, which is easy and generally all you need.
screen.availWidth
only gives me 1920
, which is the width of the primary screen, as noted in my answer, screen doesn't give you dimensions for anything other than the primary screen. –
Deficiency Yes there is a way, as long as the monitor the user is switching over to has different dimensions.
I use Angular, so this is how I do it.
Important notes before the code:
window.innerWidth
andwindow.innerHeight
is the size of your app's space within the browser. Whereaswindow.screen.width
andwindow.screen.height
is the size of the device (such as your monitor or laptop screen) that the browser running your app is on. So, if you listen to changes towindow.screen.height
andwindow.screen.width
, you can detect when a user has dragged the browser to another monitor, as long as that monitor has either a different height and or width.- You can use either
screen.width
andscreen.height
orwindow.screen.width
andwindow.screen.height
, either will get you the same. We'll usescreen
instead ofwindow.screen
since it is shorter. - The
window:resize
event at least in Angular gets triggered not just when a user expands or contracts the browser window, but also when a change in a monitor's or device's screen dimensions occurs.
import { Component, OnInit, HostListener } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
screenHeight: number
screenWidth: number
constructor() {
this.screenHeight = screen.height
this.screenWidth = screen.width
}
ngOnInit(): void { }
@HostListener('window:resize', ['$event'])
onResize() {
// if user has switched from one monitor size to another, then screen.height and screen.width will have changed
if(this.screenHeight != screen.height || this.screenWidth != screen.width) {
console.log("Congratulations, this must be a different monitor/device")
this.screenWidth = screen.width
this.screenHeight = screen.height
}
}
}
© 2022 - 2024 — McMap. All rights reserved.
device-height
anddevice-width
rules. You can attach event listeners to be notified when those values changes. Maybe it can help you. Read here. – Ardeth