Last Updated: April 03, 2019
· 2fdevs

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:


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

18 Responses
Add your response


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 ·

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 ·

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 ·

@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 ·

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 ·

Hi @robsonsobral

Thank you!

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

over 1 year ago ·

@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 ·

Much better!

I've changed it :)

over 1 year ago ·

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 ·

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

over 1 year ago ·

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

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 ·

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 ·

@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 ·

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 :)

over 1 year ago ·

Test Link Is Broken/Invalid

over 1 year ago ·

window.orientation has been deprecated: https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation#Browser_compatibility
Thanks for the post anyway

11 months ago ·

MDN says window.orientation is deprecated and will be dropped any time later. So, use of it is unreliable.

8 months ago ·

There is a a really nice API for this. You can detect the device type, brand, name, and more… and it is for free! You can find it on https://www.theapifactory.com

Hope this helps :)

about 2 months ago ·