In Javascript it is better to use screen.width
and screen.height
. These two values are available in all modern browsers. They give the real dimensions of the screen, even if the browser has been scaled down when the app fires up. window.innerWidth
changes when the browser is scaled down, which can't happen on mobile devices but can happen on PCs and laptops.
The values of screen.width
and screen.height
change when the mobile device flips between portrait and landscape modes, so it is possible to determine the mode by comparing the values. If screen.width
is greater than 1280px you're dealing with a PC or laptop.
You can construct an event listener in Javascript to detect when the two values are flipped. The portrait screen.width values to concentrate on are 320px (mainly iPhones), 360px (most other phones), 768px (small tablets) and 800px (regular tablets).