655
| Using Regex (from detectmobilebrowsers.com):
Here's a function that uses an insanely long and comprehensive regex which returns a true or false value depending on whether or not the user is browsing with a mobile.
For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:
The Original Answer
You can do this by simply running through a list of devices and checking if the useragent matches anything like so:
However since you believe that this method is unreliable, You could assume that any device that had a resolution of 800x600 or less was a mobile device too, narrowing your target even more (although these days many mobile devices have much greater resolutions than this)
i.e
Reference:
| |||
211
|
How about:
...since smartphones usually support this property and desktop browsers don't.
| ||||||||||||||||||||
|
69
|
How to use
To check to see if the user is on a specific mobile device:
Enhanced version on github : https://github.com/smali-kazmi/detect-mobile-browser
| ||||||||||||||||||||
|
14
|
Here is a simple solution from the source of facebook's slingshot
| ||
9
|
To add an extra layer of control I use the HTML5 storage to detect if it is using mobile storage or desktop storage. If the browser does not support storage I have an array of mobile browser names and I compare the user agent with the browsers in the array.
It is pretty simple. Here is the function:
| ||||||||||||
|
9
|
Feature detection is much better than trying to figure out which device you are on and very hard to keep up with new devices coming out all the time, a library like Modernizr lets you know if a particular feature is available or not.
| ||||
|
8
|
How about something like this?
| ||||||||||||||||||||
|
6
|
Came here looking for a simple, clean way to detect "touch screens devices", which I class as mobile and tablets. Did not find a clean choice in the current answers but did work out the following which may also help someone.
| ||||||||||||||||||||
|
6
|
Here is a userAgent solution that is more efficent than match...
| ||||
|
3
|
Here is my re-thought solution for the problem. Still not perfect. The only true solution would be if the device manufacturers start to take seriously the "Mobile" and "Tablet" user-agent strings. But probably this will only happen in our wildest dreams...
JS:
What happens when the Nexus 7 tablet only have the Android UA string? First, the Mobile become true, than later on the Tablet also become true, but Tablet will delete the Mobile UA string from the body tag.
CSS:
alert lines added for development. Chrome console can emulate many handheld devices. Test there.
I welcoming device name suggestions.
EDIT:
Just don't use this, use feature detection instead.
| |||
3
|
I advise you check out http://wurfl.io/
In a nutshell, if you import a tiny JS file:
you will be left with a JSON object that looks like:
(that's assuming you are using a Nexus 7, of course) and you will be able to do things like:
This is what you are looking for.
Disclaimer: I work for the company that offers this free service. Thanks.
| ||||||||
|
3
|
what about using "window.screen.width" ?
or
I guess this is the best way because there is a new mobile device every day !
(although I think it's not that supported in old browsers, but give it a try :) )
| |||
1
|
Here is he full function
| ||||||||
|
1
|
also you can follow this tutorial to detect a specific mobile. Click here.
| ||||
0
|
The best must be :
But like like Yoav Barnea says...
After this 3 tests, i hope var isMobile is... ok
| ||||||||||||
|
0
|
There's no perfect solution for detecting whether JS code is executed on a mobile browser, but the following three options should work in most cases.
Option 1 : browser sniffing
This particular browser sniffing code is that of a library called isMobile.
Option 2 : window.orientation
Test is
window.orientation is defined :
| |||
0
|
This could also be a solution.
If you use both methods, you will get a perfect way to detect different devices.
| ||
0
|
In any case, checking for type of device must be called just ONE TIME: your phone can't surprisingly stay a desktop in a moment :)
So, code for checking by userAgent, suggested here sometime ago, have to be look like that:
| ||
0
|
Here's an ECMAScript 6 solution (TypeScript ready)
|
|android|ipad|playbook|silk
to the first regex. – Gras Double Jun 7 '13 at 0:28