I found this function at StackOverFlow and I think that is brilliant. This function checks if window.orientation exists, because usually desktop computers and laptops didn't have it usually returns true on mobile devices.
I've used it in several projects and I could say that works like a charm. However you must take into account that maybe it's not 100% reliable and some devices could give you "false" (I haven't found any problem yet).
function isMobileDevice() {
return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
};
I've tested this function with good results in Windows 7 and Mac OS X browsers (Chrome, Firefox, Safari, IE9/10), Android 4, Windows Phone 8 and iOS 6.
If you want to test a device enter in this page:
http://twofuckingdevelopers.com/examples/ismobiledevice/
In case that your device isn't supported, add a comment and we will try to fix it and update the function.
List of devices tested and confirmed working:
- Safari, Chrome, IE10 and Firefox Windows 7/8 browsers
- Safari iOS
- Chrome iOS
- Opera Coast iOS
- Android Stock Browser
- Chrome for Android
- Firefox for Android
- Opera for Android
- Dolphin for Android
- IE Windows Phone 7.8 and Windows Phone 8
You say ". . . usually desktop computers and laptops didn't have it. . ."
On which devices have you found window.orientation to be defined?