[Solved-5 Solutions] Best way to detect a mobile device - javascript tutorial
Problem:
What is the best way to detect a mobile device in jQuery ?
Solution 1:
To detect mobile device is simple in javascript instead of using jquery.
Using jQuery is more accessible.
Now $.browser will return "device" for all above devices.
Note: $.browser removed on jQuery v1.9.1. But you can use this by using jQuery migration plugin Code
Solution 2:
Using CSS file:
In jQuery/Javascript file:
- Depending on the screen size to do show/hide elements by using CSS Media Queries.
- For example, In mobile version we don't want to activate the Facebook Like Box, because it loads all those profile images and stuff.
- It is not useful for mobile visitors. So, besides hiding the container element, an inside the jQuery code block:
Solution 3:
This code is simple and effective.
It doesn't take into account the case for laptops with touchscreen.
Solution 4:
It is desired to know which brand device a client is using in order to show content specific to that device, like a link to the iPhone store or the Android market. It Only shows browser capabilities, like HTML5, or Flash.
Here UserAgent solution in jQuery to display a different class for each device type:
Solution 5:
The string “Mobi” anywhere in the User Agent to detect a mobile device.
Using this code.
This will match all common mobile browser like mobile Mozilla, Safari, IE, Opera, Chrome, etc.
For Android: