Javascript to detect mobile browser screen width?

user378132 picture user378132 · Dec 14, 2011 · Viewed 41.8k times · Source

i used screen.width to get the width of the browser. it worked fine with iphone safari but didn't work in android phone(it showed 800 width for android browser).

Is there any compatible way to get the screen width. I dont want to use UserAgent to check if its mobile browser. would like to use a javascript for that and logic should include screen width.

Answer

SlavaNov picture SlavaNov · Dec 14, 2011

It's know issue - see here

When page first loads the screen.width and screen.height are wrong. Try a timeout like this:

setTimeout(CheckResolution, 300);

Where CheckResolution is your function.