javascript - What are best practices for detecting pixel ratio/density? -


i using javascript mobile device detection on website, allows me serve different content mobile or desktop users.

currently use window.devicepixelratio , screen.width work out if user if on mobile device or not, so:

var ismobilescreenwidth = ((screen.width / window.devicepixelratio) < 768) 

768px point @ define mobile or desktop 767 , below mobile , 768 , above desktop.

this works perfectly, have come across issue firefox, when firefox zoomed in , out changes window.devicepixelratio, so:

zoom = 30%, window.devicepixelratio = 0.3 zoom = 100%, window.devicepixelratio = 1.0 zoom = 300%, window.devicepixelratio = 3.0 

this causes me problem because users have browser zoomed in on firefox mobile version of site.

i wondering if knew of different or better way of getting pixel density separate desktop browsers.

i use small amount of user agent detection because massive job keep changing list of mobile user agents not possible me depend on both screen resolution , user agent @ same time.

if has ideas , can awesome.

update:

i have come across this:

window.screen.availwidth / document.documentelement.clientwidth 

this quick bit of math suggested in post:

window.devicepixelratio not work in ie 10 mobile?

i have tested , work in firefox, , solves problem, but, unfortunately causes problem in chrome, so:

chrome zoom = 100%, window.devicepixelratio = 1.0, window.screen.availwidth / document.documentelement.clientwidth = 3.0 

i cannot seem find solid solution works everything.

you should leverage manufacturer's hint via <meta name="viewport" content="width=device-width"/> or @-ms-viewport {width:device-width} feature. exposes default zoom device manufacturer considers optimal given pixel density of screen. after that, when call window.innerwidth give original equation intended without relying on measurement of pixel density.

avoid relying on window.devicepixelratio anything. meaning , value returns in state of flux , make right based around break soon.

note: meta viewport works on android, ios, , windows phone 8. @-ms-viewport works (properly) on ie10 metro , can interfere proper meta viewport behavior on windows phone 8.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -