i817wa
Last Updated: October 10, 2016
·
20.69K
· 2fdevs
C8ca661b80c139c855be32cc2024812e

One line function to detect mobile devices with JavaScript

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
Say Thanks
Respond

14 Responses
Add your response

10345
Headshot cropped

You say ". . . usually desktop computers and laptops didn't have it. . ."
On which devices have you found window.orientation to be defined?

over 1 year ago ·
10350
C8ca661b80c139c855be32cc2024812e

For now this trick have been worked an all devices I've tested. I didn't have any problem on Android, iOS, Mac OS X, Windows 7 and 8.

over 1 year ago ·
10365
0 nfyu8txkcy7l9htqq8v08golvgypn tq96e18gxteze wf5n43u8aci7u1puckp4vxyt753o6cjr

I tested today and it works fine on Opera Coast for iPad and Safari for iOS7, but not on InternetExplorer for Windows Phone 7.8 and 8.

Is it possible to add a compatibility table to your tip?

over 1 year ago ·
10370
C8ca661b80c139c855be32cc2024812e

@robsonsobral tables are not working right now, meanwhile I've created a list. If you could please tell me Windows Phone 7.8 and 8 user agents I could extend the function and add support for those devices too.

over 1 year ago ·
10371
0 nfyu8txkcy7l9htqq8v08golvgypn tq96e18gxteze wf5n43u8aci7u1puckp4vxyt753o6cjr

Sure, @2fdevs!

Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 820)
Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; NOKIA; Lumia 800)

And you always can check this.

over 1 year ago ·
10373
C8ca661b80c139c855be32cc2024812e

Hi @robsonsobral

Thank you!

I've updated the function and added an online tester :)

over 1 year ago ·
10376
0 nfyu8txkcy7l9htqq8v08golvgypn tq96e18gxteze wf5n43u8aci7u1puckp4vxyt753o6cjr

@2fdevs, do you mind if I simplify your function a little bit?

return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
over 1 year ago ·
10377
C8ca661b80c139c855be32cc2024812e

Much better!

I've changed it :)

over 1 year ago ·
10605

How about testing for actual touch events like this:

var supportsTouch = 'ontouchstart' in window || navigator.msMaxTouchPoints;

It works on all of the browsers you've listed too

over 1 year ago ·
10656
C8ca661b80c139c855be32cc2024812e

That returns undefined in Windows 8 touch based devices. I will do more tests however.

over 1 year ago ·
10824
0 nfyu8txkcy7l9htqq8v08golvgypn tq96e18gxteze wf5n43u8aci7u1puckp4vxyt753o6cjr

Surface Pro 1 isn't considered mobile, despite the changes of screen orientation.

Chrome:
Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36

Firefox 23
Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0

Internet Explorer 10
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch; .NET4.0E; .NET4.0C; Tablet PC 2.0; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729)

But I don't know if we should consider Surface as a mobile or not.

over 1 year ago ·
10825
C8ca661b80c139c855be32cc2024812e

That's a good question, I think that we should not consider Surface as a mobile, but it is a really hybrid device so it could fit in this category.

Anyway, I'm pretty sure that anyone could modify the function to whatever they need.

over 1 year ago ·
10826
0 nfyu8txkcy7l9htqq8v08golvgypn tq96e18gxteze wf5n43u8aci7u1puckp4vxyt753o6cjr

@2fdevs, my friend who owns one and tested this for me said he consider it as a "mini ultra book", because isn't really "mobile". So...

over 1 year ago ·
28228

Hi, this is something ive been looking for. Its now Oct 2016. Is this code still reliable to use with most of todays browsers/devices? Im a newbie coder so at the moment its beyond me to figure it out myself :)

10 months ago ·