Android browser's screen.width, screen.height & window.innerWidth & window.innerHeight are unreliable

android, dom, html, javascript

Solution

On Android, window.outerWidth and window.outerHeight are reliably the screen size. Depending on your version of Android, innerWidth/Height is usually incorrect.

Here's a really good writeup on the situation.

Problem

I'm working on a web app that is targeted to browsers on desktop, tablet and smartphone. The web app has a light box implemented using Colorbox with an `iframe`. When the browser window is resized or the tablet/phone has it's orientation changed, Javascript code queries the window dimensions so that it can resize some elements of the light box. The issue I'm having is that everything works fine on desktop (Windows: IE, Firefox, Chrome, Mac: Safari), iPad & iPhone but not on the Android smartphone (HTC) and Android Emulator. Android always returns different values for `screen.width`, `screen.height`, `window.innerWidth` & `window.innerHeight` when they're queried from inside the window's resize event, which fires multiple times. Why is the Android browser returning such a wide variance in values and how can I reliably detect the width and height of the browser window?

Original source