Reputation: 5619
how Can I detect a mobile browser with Jquery 2.1.4?
jQuery.browser.mobile
dosn't work anymore
Upvotes: 0
Views: 1333
Reputation: 821
As Jacob mentioned, you should identify WHAT you are trying to detect. Just detecting that it's a "mobile" device isn't really useful.
Upvotes: 1
Reputation: 51
First of all, jQuery has removed jQuery.browser effectively from v1.9. If you want to use it, you will have to utilize the jQuery migrate plugin to re-enable the functionality of older jQuery, including $.browser
.
Second, I don't believe jQuery.browser
ever had the possibility of jQuery.browser.mobile
.
Third, the reason it's removed from jQuery is that you need to ask a different question: How do you define a mobile browser? Is it the screen resolution? You could resize your screen on a laptop aswell. The touch interface? Laptops have these as well. Browser version? I doubt there's a difference between Chrome for Android and Chrome for Windows.
But there is a solution!
jQuery advises the use of "feature detection" instead, and this is a really good idea. What it means is basically to check for certain features, when they are needed. For instance if you want to be able to scroll something using your fingers, say a picture slider with jQuery, you could check for the presence of touch events: var hasTouch = ('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch;
If you want to design for something dependent of screen resolution, you should use media queries with CSS.
So it all comes down to this: Feature detection > mobile detection. Oh, and you probably should not be concerned about what browser your visitors are using, while all newer browsers are for the most part keeping to the standards. Your main concern, without knowing your use-case, should be screen resolutions, or rather viewport resolutions.
Upvotes: 2