Is there a way to get the users device width, as opposed to viewport width, using javascript?
CSS media queries offer this, as I can say
@media screen and (max-width:640px) {
/* ... */
}
and
@media screen and (max-device-width:960px) {
/* ... */
}
This is useful if I'm targeting smartphones in landscape orientation. For example, on iOS a declaration of max-width:640px
will target both landscape and portrait modes, even on an iPhone 4. This is not the case for Android, as far as I can tell, so using device-width in this instance successfully targets both orientations, without targeting desktop devices.
However, if I'm invoking a javascript binding based on device width, I appear to be limited to testing the viewport width, which means an extra test as in the following,
if ($(window).width() <= 960 && $(window).height <= 640) { /* ... */ }
This doesn't seem elegant to me, given the hint that device width is available to css.
Modernizr
can help you do this "the clean & generic way": stackoverflow.com/questions/25935686/… . Regarding the 1st comment: you may want to google "Modernizr vs <otherLib> media query" to find out what works best for your use case – Adrien Be