Detecting a mobile browser












682















I'm looking for a function which return boolean value if user has mobile browser or not.



I know that I can use navigator.userAgent and write that function by using regex, but user-agents are too various for different platforms. I doubt that match all possible devices would be easy, and I think this problem has been solved before many times so there should be some kind of complete solution for such task.



I was looking at this site, but sadly the script is so cryptic that I have no idea how to use it for my purpose, which is to create a function which return true / false.










share|improve this question




















  • 5





    Related: stackoverflow.com/questions/3514784/….

    – Frédéric Hamidi
    Jul 8 '12 at 8:16








  • 2





    Try reading this. stackoverflow.com/questions/743129/…

    – KyelJmD
    Jul 8 '12 at 8:25






  • 4





    @Thrustmaster: It really wouldn't. Serving different JS to different browsers means you have to add Vary: User-Agent to your response, otherwise caching proxies will store one version and send it to the other kind of browser. But Vary: User-Agent makes the response uncachable in IE.

    – bobince
    Jul 8 '12 at 8:51






  • 13





    @ave: What are you trying to do by detecting a "mobile" browser? The distinction is highly arguable in today's world of tablets and hybrid computing devices. Are you looking to detect small screens, and present a different UI in that case? Are you looking to detect low-bandwidth connectivity? Are you looking to detect touch interfaces?

    – bobince
    Jul 8 '12 at 8:53








  • 2





    So i've updated my answer to use the detectmobilebrowsers.com javascript method but return a boolean value if anyone still needs this. ( just in case ). Happy Detecting :)

    – Michael Zaporozhets
    Feb 3 '13 at 15:28
















682















I'm looking for a function which return boolean value if user has mobile browser or not.



I know that I can use navigator.userAgent and write that function by using regex, but user-agents are too various for different platforms. I doubt that match all possible devices would be easy, and I think this problem has been solved before many times so there should be some kind of complete solution for such task.



I was looking at this site, but sadly the script is so cryptic that I have no idea how to use it for my purpose, which is to create a function which return true / false.










share|improve this question




















  • 5





    Related: stackoverflow.com/questions/3514784/….

    – Frédéric Hamidi
    Jul 8 '12 at 8:16








  • 2





    Try reading this. stackoverflow.com/questions/743129/…

    – KyelJmD
    Jul 8 '12 at 8:25






  • 4





    @Thrustmaster: It really wouldn't. Serving different JS to different browsers means you have to add Vary: User-Agent to your response, otherwise caching proxies will store one version and send it to the other kind of browser. But Vary: User-Agent makes the response uncachable in IE.

    – bobince
    Jul 8 '12 at 8:51






  • 13





    @ave: What are you trying to do by detecting a "mobile" browser? The distinction is highly arguable in today's world of tablets and hybrid computing devices. Are you looking to detect small screens, and present a different UI in that case? Are you looking to detect low-bandwidth connectivity? Are you looking to detect touch interfaces?

    – bobince
    Jul 8 '12 at 8:53








  • 2





    So i've updated my answer to use the detectmobilebrowsers.com javascript method but return a boolean value if anyone still needs this. ( just in case ). Happy Detecting :)

    – Michael Zaporozhets
    Feb 3 '13 at 15:28














682












682








682


379






I'm looking for a function which return boolean value if user has mobile browser or not.



I know that I can use navigator.userAgent and write that function by using regex, but user-agents are too various for different platforms. I doubt that match all possible devices would be easy, and I think this problem has been solved before many times so there should be some kind of complete solution for such task.



I was looking at this site, but sadly the script is so cryptic that I have no idea how to use it for my purpose, which is to create a function which return true / false.










share|improve this question
















I'm looking for a function which return boolean value if user has mobile browser or not.



I know that I can use navigator.userAgent and write that function by using regex, but user-agents are too various for different platforms. I doubt that match all possible devices would be easy, and I think this problem has been solved before many times so there should be some kind of complete solution for such task.



I was looking at this site, but sadly the script is so cryptic that I have no idea how to use it for my purpose, which is to create a function which return true / false.







javascript browser-detection mobile-browser platform-detection






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 1 '18 at 10:34









John Slegers

27.8k13148129




27.8k13148129










asked Jul 8 '12 at 8:13









aveave

4,29351730




4,29351730








  • 5





    Related: stackoverflow.com/questions/3514784/….

    – Frédéric Hamidi
    Jul 8 '12 at 8:16








  • 2





    Try reading this. stackoverflow.com/questions/743129/…

    – KyelJmD
    Jul 8 '12 at 8:25






  • 4





    @Thrustmaster: It really wouldn't. Serving different JS to different browsers means you have to add Vary: User-Agent to your response, otherwise caching proxies will store one version and send it to the other kind of browser. But Vary: User-Agent makes the response uncachable in IE.

    – bobince
    Jul 8 '12 at 8:51






  • 13





    @ave: What are you trying to do by detecting a "mobile" browser? The distinction is highly arguable in today's world of tablets and hybrid computing devices. Are you looking to detect small screens, and present a different UI in that case? Are you looking to detect low-bandwidth connectivity? Are you looking to detect touch interfaces?

    – bobince
    Jul 8 '12 at 8:53








  • 2





    So i've updated my answer to use the detectmobilebrowsers.com javascript method but return a boolean value if anyone still needs this. ( just in case ). Happy Detecting :)

    – Michael Zaporozhets
    Feb 3 '13 at 15:28














  • 5





    Related: stackoverflow.com/questions/3514784/….

    – Frédéric Hamidi
    Jul 8 '12 at 8:16








  • 2





    Try reading this. stackoverflow.com/questions/743129/…

    – KyelJmD
    Jul 8 '12 at 8:25






  • 4





    @Thrustmaster: It really wouldn't. Serving different JS to different browsers means you have to add Vary: User-Agent to your response, otherwise caching proxies will store one version and send it to the other kind of browser. But Vary: User-Agent makes the response uncachable in IE.

    – bobince
    Jul 8 '12 at 8:51






  • 13





    @ave: What are you trying to do by detecting a "mobile" browser? The distinction is highly arguable in today's world of tablets and hybrid computing devices. Are you looking to detect small screens, and present a different UI in that case? Are you looking to detect low-bandwidth connectivity? Are you looking to detect touch interfaces?

    – bobince
    Jul 8 '12 at 8:53








  • 2





    So i've updated my answer to use the detectmobilebrowsers.com javascript method but return a boolean value if anyone still needs this. ( just in case ). Happy Detecting :)

    – Michael Zaporozhets
    Feb 3 '13 at 15:28








5




5





Related: stackoverflow.com/questions/3514784/….

– Frédéric Hamidi
Jul 8 '12 at 8:16







Related: stackoverflow.com/questions/3514784/….

– Frédéric Hamidi
Jul 8 '12 at 8:16






2




2





Try reading this. stackoverflow.com/questions/743129/…

– KyelJmD
Jul 8 '12 at 8:25





Try reading this. stackoverflow.com/questions/743129/…

– KyelJmD
Jul 8 '12 at 8:25




4




4





@Thrustmaster: It really wouldn't. Serving different JS to different browsers means you have to add Vary: User-Agent to your response, otherwise caching proxies will store one version and send it to the other kind of browser. But Vary: User-Agent makes the response uncachable in IE.

– bobince
Jul 8 '12 at 8:51





@Thrustmaster: It really wouldn't. Serving different JS to different browsers means you have to add Vary: User-Agent to your response, otherwise caching proxies will store one version and send it to the other kind of browser. But Vary: User-Agent makes the response uncachable in IE.

– bobince
Jul 8 '12 at 8:51




13




13





@ave: What are you trying to do by detecting a "mobile" browser? The distinction is highly arguable in today's world of tablets and hybrid computing devices. Are you looking to detect small screens, and present a different UI in that case? Are you looking to detect low-bandwidth connectivity? Are you looking to detect touch interfaces?

– bobince
Jul 8 '12 at 8:53







@ave: What are you trying to do by detecting a "mobile" browser? The distinction is highly arguable in today's world of tablets and hybrid computing devices. Are you looking to detect small screens, and present a different UI in that case? Are you looking to detect low-bandwidth connectivity? Are you looking to detect touch interfaces?

– bobince
Jul 8 '12 at 8:53






2




2





So i've updated my answer to use the detectmobilebrowsers.com javascript method but return a boolean value if anyone still needs this. ( just in case ). Happy Detecting :)

– Michael Zaporozhets
Feb 3 '13 at 15:28





So i've updated my answer to use the detectmobilebrowsers.com javascript method but return a boolean value if anyone still needs this. ( just in case ). Happy Detecting :)

– Michael Zaporozhets
Feb 3 '13 at 15:28












31 Answers
31






active

oldest

votes













1 2
next












1045














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.



window.mobilecheck = function() {
var check = false;
(function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};


For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:



window.mobileAndTabletcheck = function() {
var check = false;
(function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
return check;
};




The Original Answer



You can do this by simply running through a list of devices and checking if the useragent matches anything like so:



function detectmob() { 
if( navigator.userAgent.match(/Android/i)
|| navigator.userAgent.match(/webOS/i)
|| navigator.userAgent.match(/iPhone/i)
|| navigator.userAgent.match(/iPad/i)
|| navigator.userAgent.match(/iPod/i)
|| navigator.userAgent.match(/BlackBerry/i)
|| navigator.userAgent.match(/Windows Phone/i)
){
return true;
}
else {
return false;
}
}


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



function detectmob() {
if(window.innerWidth <= 800 && window.innerHeight <= 600) {
return true;
} else {
return false;
}
}


Reference:




  • Detecting Browser and Devices with javascript






share|improve this answer





















  • 20





    Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

    – Richard Lovejoy
    Mar 27 '13 at 18:28






  • 39





    @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

    – Michael Zaporozhets
    Mar 28 '13 at 11:14






  • 40





    From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

    – Gras Double
    Jun 7 '13 at 0:28








  • 11





    Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

    – molokoloco
    Jun 26 '13 at 22:26






  • 28





    Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

    – marksyzm
    Aug 15 '13 at 10:07



















285














How about:



if (typeof window.orientation !== 'undefined') { ... }


...since smartphones usually support this property and desktop browsers don't.



EDIT: As @Gajus pointed out, this solution is now deprecated and shouldn't be used (https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation)






share|improve this answer





















  • 11





    this is actually super unique and awesome, do you mind if I add it to my answer?

    – Michael Zaporozhets
    Jan 30 '13 at 0:34








  • 73





    This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

    – Dave Hilditch
    Feb 20 '13 at 17:33






  • 9





    as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

    – yoav barnea
    Mar 2 '13 at 20:01








  • 2





    Love it and works perfectly, thankyou. For my solution I was just after simple.

    – Bojangles
    Jun 18 '13 at 6:19






  • 25





    window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

    – Gajus
    Feb 8 '16 at 10:07



















96














var isMobile = {
Android: function() {
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function() {
return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};


How to use



if( isMobile.any() ) alert('Mobile');


To check to see if the user is on a specific mobile device:



if( isMobile.iOS() ) alert('iOS');


Ref: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript



Enhanced version on github : https://github.com/smali-kazmi/detect-mobile-browser






share|improve this answer


























  • Why not make any() a for...in loop of ORed isMobile members?

    – SomeShinyObject
    Mar 1 '14 at 15:43











  • @ChristopherW i had created its plugin and modified code as you advised

    – Mudaser Ali
    Apr 30 '14 at 19:57






  • 1





    Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

    – Rob_vH
    Mar 12 '15 at 14:33






  • 2





    @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

    – Mudaser Ali
    Mar 13 '15 at 19:45











  • This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

    – CSS
    Aug 11 '15 at 18:11





















43














Here is a simple solution from the source of facebook's slingshot



var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
if (isMobile) {
/* your code here */
}





share|improve this answer
























  • Nice. Very useful in certain situations

    – Chuck Le Butt
    Mar 2 '18 at 20:16



















16














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.



var touchDevice = ('ontouchstart' in document.documentElement);


Edit: To support desktops with a touch screen and mobiles at the same time you can use the following:



var touchDevice = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);





share|improve this answer





















  • 6





    What if desktop's monitor supports touch?

    – Anton Kuzmin
    Aug 1 '16 at 1:25











  • @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

    – Tigger
    Aug 1 '16 at 9:09











  • (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

    – Cody
    Dec 16 '16 at 21:18






  • 1





    Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

    – J.T. Taylor
    Jul 15 '17 at 0:31






  • 1





    @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

    – Tigger
    Jul 15 '17 at 0:43



















13














There's no perfect solution for detecting whether JS code is executed on a mobile browser, but the following two options should work in most cases.



Option 1 : browser sniffing






!function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

alert(isMobile.any ? 'Mobile' : 'Not mobile');





This particular browser sniffing code is that of a library called isMobile.





Option 2 : window.orientation



Test if window.orientation is defined :






var isMobile = window.orientation > -1;
alert(isMobile ? 'Mobile' : 'Not mobile');







Note



Not all touchscreen devices are mobile and vice versa. So, if you want to implement something specifically for touchscreen, you shouldn't test if your browser is run on a mobile device but rather whether the devices has touchscreen support :






var hasTouchscreen = 'ontouchstart' in window;
alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');








share|improve this answer


























  • Orientation approach is really nice! ))

    – Maxim
    Oct 19 '17 at 11:57











  • I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

    – skwidbreth
    Apr 10 '18 at 17:58






  • 2





    Orientation approach is NOT nice because Windows 8 and higher can change orientation.

    – Heitor
    Aug 4 '18 at 6:58



















12














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:



// Used to detect whether the users browser is an mobile browser
function isMobile() {
///<summary>Detecting whether the browser is a mobile browser or desktop browser</summary>
///<returns>A boolean value indicating whether the browser is a mobile browser or not</returns>

if (sessionStorage.desktop) // desktop storage
return false;
else if (localStorage.mobile) // mobile storage
return true;

// alternative
var mobile = ['iphone','ipad','android','blackberry','nokia','opera mini','windows mobile','windows phone','iemobile'];
for (var i in mobile) if (navigator.userAgent.toLowerCase().indexOf(mobile[i].toLowerCase()) > 0) return true;

// nothing found.. assume desktop
return false;
}





share|improve this answer





















  • 1





    Can I please have a reason for the downvote?

    – Rasmus Søborg
    Jun 27 '13 at 12:46






  • 3





    I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

    – shuckster
    Dec 28 '13 at 15:33








  • 3





    Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

    – Jeffrey Gilbert
    Jun 2 '14 at 22:19



















10














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.






share|improve this answer



















  • 14





    You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

    – Jonathan Allard
    Jun 1 '16 at 19:10






  • 1





    This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

    – twicejr
    Aug 4 '17 at 14:24



















10














How about something like this?



if(
(screen.width <= 640) ||
(window.matchMedia &&
window.matchMedia('only screen and (max-width: 640px)').matches
)
){
// Do the mobile thing
}





share|improve this answer
























  • Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

    – John Slegers
    Mar 12 '16 at 2:30











  • Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

    – stujo
    Mar 23 '16 at 23:35











  • Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

    – John Slegers
    Mar 24 '16 at 7:33








  • 1





    Thanks John :) Yeay Progress!

    – stujo
    Apr 12 '16 at 3:45






  • 2





    Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

    – ʇsәɹoɈ
    Apr 21 '16 at 2:16





















8














Here is a userAgent solution that is more efficent than match...



function _isMobile(){
// if we want a more complete list use this: http://detectmobilebrowsers.com/
// str.test() is more efficent than str.match()
// remember str.test is case sensitive
var isMobile = (/iphone|ipod|android|ie|blackberry|fennec/).test
(navigator.userAgent.toLowerCase());
return isMobile;
}





share|improve this answer



















  • 7





    the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

    – xec
    Feb 5 '13 at 17:01





















6














A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.



Plain JavaScript Code:



function isMobile() {
try{ document.createEvent("TouchEvent"); return true; }
catch(e){ return false; }
}

if (isMobile()) {
# do whatever you wanna do!
}


This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.



I am not sure if a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.






share|improve this answer





















  • 3





    Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

    – blissfool
    Jun 4 '18 at 20:59













  • @blissfool i guess this will not be the right way for detecting mobile devices then.

    – Neo Morina
    Jun 12 '18 at 7:08













  • Unfortunately, no. But, it might still be a viable option for a very limited use case.

    – blissfool
    Jun 13 '18 at 14:51











  • never write code, that is based on an exception, that will be throwen for sure in any case...

    – Sivic
    Nov 15 '18 at 8:30



















5














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.



window.onload = userAgentDetect;
function userAgentDetect() {
if(window.navigator.userAgent.match(/Mobile/i)
|| window.navigator.userAgent.match(/iPhone/i)
|| window.navigator.userAgent.match(/iPod/i)
|| window.navigator.userAgent.match(/IEMobile/i)
|| window.navigator.userAgent.match(/Windows Phone/i)
|| window.navigator.userAgent.match(/Android/i)
|| window.navigator.userAgent.match(/BlackBerry/i)
|| window.navigator.userAgent.match(/webOS/i)) {
document.body.className += ' mobile';
alert('True - Mobile - ' + navigator.userAgent);
} else {
alert('False - Mobile - ' + navigator.userAgent);
}
if(window.navigator.userAgent.match(/Tablet/i)
|| window.navigator.userAgent.match(/iPad/i)
|| window.navigator.userAgent.match(/Nexus 7/i)
|| window.navigator.userAgent.match(/Nexus 10/i)
|| window.navigator.userAgent.match(/KFAPWI/i)) {
document.body.className -= ' mobile';
document.body.className += ' tablet';
alert('True - Tablet - ' + navigator.userAgent);
} else {
alert('False - Tablet - ' + navigator.userAgent);
}
}


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:



body.tablet { background-color: green; }
body.mobile { background-color: red; }


alert lines added for development. Chrome console can emulate many handheld devices. Test there.



EDIT:



Just don't use this, use feature detection instead. There are so many devices and brands out there that targeting a brand NEVER will be the right solution.






share|improve this answer

































    4














    I advise you check out http://wurfl.io/



    In a nutshell, if you import a tiny JS file:



    <script type='text/javascript' src="//wurfl.io/wurfl.js"></script>


    you will be left with a JSON object that looks like:



    {
    "complete_device_name":"Google Nexus 7",
    "is_mobile":true,
    "form_factor":"Tablet"
    }


    (that's assuming you are using a Nexus 7, of course) and you will be able to do things like:



    if(WURFL.form_factor == "Tablet"){
    //dostuff();
    }


    This is what you are looking for.



    Disclaimer: I work for the company that offers this free service. Thanks.






    share|improve this answer



















    • 1





      And howcome this does not recognize safari on iphone ?

      – Amyth
      Sep 6 '16 at 2:05











    • Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

      – Luca Passani
      Sep 7 '16 at 13:11











    • I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

      – Mike Wells
      Nov 17 '17 at 16:51











    • I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

      – Luca Passani
      Nov 20 '17 at 2:01



















    4














    Once the element gains focus, you immediately blur it. Bootstrap-datepicker, which is a very popular and well-maintained component with almost 10,000 stars in GitHub, uses this approach:



    if (window.navigator.maxTouchPoints || 'ontouchstart' in document) {
    this.input.blur();
    }


    https://github.com/uxsolutions/bootstrap-datepicker



    Thanks to Tigger for assistance.






    share|improve this answer

































      4














      As many have stated, relying on the moving target of the user agent data is problematic. The same can be said for counting on screen size. My approach is borrowed from a CSS technique to determine if the interface is touch. Using pure javascript (support by all modern browsers), a media query you can easily determine when the device is mobile.



      function isMobile() {

      if(typeof window.matchMedia != 'undefined' || typeof window.msMatchMedia != 'undefined') {
      var mq = window.matchMedia("(pointer:coarse)");
      return mq.matches;
      }
      return false;
      }





      share|improve this answer
























      • This solution works for me, and it up to date...

        – RRTW
        Oct 1 '18 at 7:18






      • 1





        What about laptops with touch enabled displays?

        – Maxim
        Oct 31 '18 at 21:32



















      3














      heres a one liner



      function isMobile() {
      return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
      };





      share|improve this answer































        2














        There is simple trick to detect whether it is a mobile device or not. Just check if the ontouchstart event exist:



        function isMobile()
        {
        return "ontouchstart" in window;
        }





        share|improve this answer
























        • Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

          – blissfool
          Jun 4 '18 at 21:01



















        2














        According to MDN's article on Browser detection using the user agent, it is encouraged to avoid this approach if possible and suggest other avenues such as feature detection.



        However, if one must use the user agent as a means to detect if the device is mobile, they suggest:




        In summary, we recommend looking for the string “Mobi” anywhere in the
        User Agent to detect a mobile device.




        Therefore, this one-liner will suffice:



        const isMobileDevice = window.navigator.userAgent.toLowerCase().includes("mobi");






        share|improve this answer


























        • .includes not sopport by IE-11

          – Pasha Oleynik
          Sep 3 '18 at 12:57











        • @PashaOleynik a polyfill can fix that

          – Maxim
          Oct 31 '18 at 21:35



















        1














        The best must be :



        var isMobile = (/Mobile/i.test(navigator.userAgent));


        But like like Yoav Barnea says...



        // Seem legit
        var isMobile = ('DeviceOrientationEvent' in window || 'orientation' in window);
        // But with my Chrome on windows, DeviceOrientationEvent == fct()
        if (/Windows NT|Macintosh|Mac OS X|Linux/i.test(navigator.userAgent)) isMobile = false;
        // My android have "linux" too
        if (/Mobile/i.test(navigator.userAgent)) isMobile = true;


        After this 3 tests, i hope var isMobile is... ok






        share|improve this answer


























        • > Firefox mobile on android doesn't seem to have "'orientation' in window"

          – molokoloco
          May 30 '13 at 12:38






        • 1





          Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

          – molokoloco
          Jun 1 '13 at 13:31













        • Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

          – B2K
          Jul 10 '15 at 14:27











        • To make it more elegant you should make all te code in just one if-else if-else if block.

          – Heitor
          Aug 4 '18 at 6:56



















        1














        Here is he full function



        function isMobile(){
        return (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i.test(navigator.userAgent||navigator.vendor||window.opera)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test((navigator.userAgent||navigator.vendor||window.opera).substr(0,4)))
        }

        jQuery.noConflict();
        jQuery(document).ready(function(){
        if(isMobile()) alert("Mobile"); else alert("Not Mobile");
        });





        share|improve this answer


























        • .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

          – raacer
          Aug 24 '15 at 21:31






        • 1





          @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

          – JackW
          Aug 27 '15 at 13:45



















        1














        //true / false
        function isMobile()
        {
        return (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) );
        }


        also you can follow this tutorial to detect a specific mobile. Click here.






        share|improve this answer
























        • Please add Mobile to your RX

          – oriadam
          Jul 11 '16 at 11:05



















        1














        what about using "window.screen.width" ?



        if (window.screen.width < 800) {
        // do something
        }


        or



        if($(window).width() < 800) {
        //do something
        }


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






        share|improve this answer





















        • 1





          What is about landscape?

          – Erick Voodoo
          Apr 10 '17 at 11:29






        • 1





          This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

          – toing_toing
          May 7 '17 at 16:43











        • A PC is essentially different from mobile devices on usability, horrable answer!!

          – Heitor
          Aug 4 '18 at 7:01



















        1














        Note that Most newer-gen mobile devices now have resolutions greater than 600x400. ie, an iPhone 6....



        Proof of test:
        ran the most upvoted and most recent posts here, with an optional check once run like so:



        (function(a){
        window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))
        })(navigator.userAgent||navigator.vendor||window.opera);

        alert("This browser was found to be a % browser.", window.isMobile ? 'mobile' : 'desktop');


        Somehow, the following results were returned on the following Browser Apps. Specs: iPhone 6S, iOS 10.3.1.



        Safari (latest): Detected it as a mobile.



        Chrome (latest): Did not detect it as a mobile.



        SO, i then tested the suggestion from Lanti (https://stackoverflow.com/a/31864119/7183483), and it did return the proper results (mobile for all iOS devices, and desktop for my Mac). Therefore, i proceeded to edit it a little since it would fire twice (for both mobile and Tablet). I then noticed when testing on an iPad, that it also returned as a mobile, which makes sense, since the Parameters that Lanti uses check the OS more than anything. Therefore, i simply moved the tablet IF statement inside the mobile check, which would return mobile is the Tablet check was negative, and tablet otherwise. I then added the else clause for the mobile check to return as desktop/laptop, since both qualify, but then noticed that the browser detects CPU and OS brand. So i added what is returned in there as part of else if statement instead. To cap it, I added a cautionary else statement in case nothing was detected.
        See bellow, will update with a test on a Windows 10 PC soon.



        Oh, and i also added a 'debugMode' variable, to easily switch between debug and normal compiling.



        Dislaimer: Full credit to Lanti, also that this was not tested on Windows Tablets... which might return desktop/laptop, since the OS is pure Windows. Will check once I find a friend who uses one.



        function userAgentDetect() {
        let debugMode = true;
        if(window.navigator.userAgent.match(/Mobile/i)
        || window.navigator.userAgent.match(/iPhone/i)
        || window.navigator.userAgent.match(/iPod/i)
        || window.navigator.userAgent.match(/IEMobile/i)
        || window.navigator.userAgent.match(/Windows Phone/i)
        || window.navigator.userAgent.match(/Android/i)
        || window.navigator.userAgent.match(/BlackBerry/i)
        || window.navigator.userAgent.match(/webOS/i)) {
        if (window.navigator.userAgent.match(/Tablet/i)
        || window.navigator.userAgent.match(/iPad/i)
        || window.navigator.userAgent.match(/Nexus 7/i)
        || window.navigator.userAgent.match(/Nexus 10/i)
        || window.navigator.userAgent.match(/KFAPWI/i)) {
        window.deviceTypeVar = 'tablet';
        if (debugMode === true) {
        alert('Device is a tablet - ' + navigator.userAgent);
        }
        } else {
        if (debugMode === true) {
        alert('Device is a smartphone - ' + navigator.userAgent);
        };
        window.deviceTypeVar = 'smartphone';
        }
        } else if (window.navigator.userAgent.match(/Intel Mac/i)) {
        if (debugMode === true) {
        alert('Device is a desktop or laptop- ' + navigator.userAgent);
        }
        window.deviceTypeVar = 'desktop_or_laptop';
        } else if (window.navigator.userAgent.match(/Nexus 7/i)
        || window.navigator.userAgent.match(/Nexus 10/i)
        || window.navigator.userAgent.match(/KFAPWI/i)) {
        window.deviceTypeVar = 'tablet';
        if (debugMode === true) {
        alert('Device is a tablet - ' + navigator.userAgent);
        }
        } else {
        if (debugMode === true) {
        alert('Device is unknown- ' + navigator.userAgent);
        }
        window.deviceTypeVar = 'Unknown';
        }
        }





        share|improve this answer

































          1














          This is just an es6 port of the accepted answer that I'm using in my project. Note that this also includes tablets.



          export const isMobile = () => {
          const vendor = navigator.userAgent || navigator.vendor || window.opera;

          return !!(
          /(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(
          vendor
          ) ||
          /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(
          vendor.substr(0, 4)
          )
          );
          };





          share|improve this answer































            1

















            return 'ontouchstart' in window && window.screen.availWidth < 768





            How about this, it expands on answer above but also checks the screen size






            share|improve this answer































              0














              This could also be a solution.



              var isMobile = false; //initiate as false

              // device detection
              if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
              || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(navigator.userAgent.substr(0,4))) isMobile = true;

              console.log('Mobile device:'+isMobile);

              var doc_h = $(document).height(); // returns height of HTML document
              var doc_w = $(document).width(); // returns width of HTML document
              console.log('Height: '+doc_h);
              console.log('width: '+doc_w);

              var iPadVertical = window.matchMedia("(width: 768px) and (height: 1024px) and (orientation: portrait)");
              var iPadHoricontal = window.matchMedia("(width: 1024px) and (height: 767px) and (orientation: landscape)");

              console.log('Height: '+doc_h);
              console.log('width: '+doc_w);

              if (iPadVertical.matches) {
              console.log('Ipad vertical detected');
              }else if (iPadHoricontal.matches){
              console.log('Ipad horicontal detected');
              }else {
              console.log('No Ipad');
              }


              If you use both methods, you will get a perfect way to detect different devices.






              share|improve this answer































                0














                Here's an ECMAScript 6 solution (TypeScript ready)



                public isMobile(): boolean {
                let check = false;
                ((a => {
                if (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0, 4))) check = true;
                }))(navigator.userAgent || navigator.vendor);
                return check;
                }





                share|improve this answer
























                • why not just return the if condition instead of having this whole check variable?

                  – Vic
                  Apr 20 '18 at 19:15



















                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:






                (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');








                share|improve this answer































                  0














                  Depends on the use case. All mobile devices require a battery. If what you're after is compute power without draining the battery use the Battery Status API:



                  navigator.getBattery().then(battery => {
                  battery.charging ? 'charging' : 'not charging';
                  });


                  If what you're looking for is presentational use matchMedia, which returns a Boolean value:



                  if (window.matchMedia("(min-width: 400px)").matches) {
                  /* the viewport is at least 400 pixels wide */
                  } else {
                  /* the viewport is less than 400 pixels wide */
                  }


                  Or combine them for an even better user experience on tablet devices.






                  share|improve this answer

































                    -1














                    If you're looking for a mobile phone, since they're portrait oriented in most cases, you can use this simple check:



                    if ($(window).width() > $(window).height())
                    alert('Landscape'); // usually tablet or desktop
                    else
                    alert('Portrait'); // usually mobile phone


                    NOTE: this is not a reliable solution, but it will work in most cases!






                    share|improve this answer





























                      1 2
                      next











                      31 Answers
                      31






                      active

                      oldest

                      votes








                      31 Answers
                      31






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes








                      1 2
                      next










                      1045














                      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.



                      window.mobilecheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };


                      For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:



                      window.mobileAndTabletcheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };




                      The Original Answer



                      You can do this by simply running through a list of devices and checking if the useragent matches anything like so:



                      function detectmob() { 
                      if( navigator.userAgent.match(/Android/i)
                      || navigator.userAgent.match(/webOS/i)
                      || navigator.userAgent.match(/iPhone/i)
                      || navigator.userAgent.match(/iPad/i)
                      || navigator.userAgent.match(/iPod/i)
                      || navigator.userAgent.match(/BlackBerry/i)
                      || navigator.userAgent.match(/Windows Phone/i)
                      ){
                      return true;
                      }
                      else {
                      return false;
                      }
                      }


                      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



                      function detectmob() {
                      if(window.innerWidth <= 800 && window.innerHeight <= 600) {
                      return true;
                      } else {
                      return false;
                      }
                      }


                      Reference:




                      • Detecting Browser and Devices with javascript






                      share|improve this answer





















                      • 20





                        Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

                        – Richard Lovejoy
                        Mar 27 '13 at 18:28






                      • 39





                        @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

                        – Michael Zaporozhets
                        Mar 28 '13 at 11:14






                      • 40





                        From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

                        – Gras Double
                        Jun 7 '13 at 0:28








                      • 11





                        Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

                        – molokoloco
                        Jun 26 '13 at 22:26






                      • 28





                        Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

                        – marksyzm
                        Aug 15 '13 at 10:07
















                      1045














                      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.



                      window.mobilecheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };


                      For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:



                      window.mobileAndTabletcheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };




                      The Original Answer



                      You can do this by simply running through a list of devices and checking if the useragent matches anything like so:



                      function detectmob() { 
                      if( navigator.userAgent.match(/Android/i)
                      || navigator.userAgent.match(/webOS/i)
                      || navigator.userAgent.match(/iPhone/i)
                      || navigator.userAgent.match(/iPad/i)
                      || navigator.userAgent.match(/iPod/i)
                      || navigator.userAgent.match(/BlackBerry/i)
                      || navigator.userAgent.match(/Windows Phone/i)
                      ){
                      return true;
                      }
                      else {
                      return false;
                      }
                      }


                      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



                      function detectmob() {
                      if(window.innerWidth <= 800 && window.innerHeight <= 600) {
                      return true;
                      } else {
                      return false;
                      }
                      }


                      Reference:




                      • Detecting Browser and Devices with javascript






                      share|improve this answer





















                      • 20





                        Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

                        – Richard Lovejoy
                        Mar 27 '13 at 18:28






                      • 39





                        @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

                        – Michael Zaporozhets
                        Mar 28 '13 at 11:14






                      • 40





                        From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

                        – Gras Double
                        Jun 7 '13 at 0:28








                      • 11





                        Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

                        – molokoloco
                        Jun 26 '13 at 22:26






                      • 28





                        Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

                        – marksyzm
                        Aug 15 '13 at 10:07














                      1045












                      1045








                      1045







                      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.



                      window.mobilecheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };


                      For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:



                      window.mobileAndTabletcheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };




                      The Original Answer



                      You can do this by simply running through a list of devices and checking if the useragent matches anything like so:



                      function detectmob() { 
                      if( navigator.userAgent.match(/Android/i)
                      || navigator.userAgent.match(/webOS/i)
                      || navigator.userAgent.match(/iPhone/i)
                      || navigator.userAgent.match(/iPad/i)
                      || navigator.userAgent.match(/iPod/i)
                      || navigator.userAgent.match(/BlackBerry/i)
                      || navigator.userAgent.match(/Windows Phone/i)
                      ){
                      return true;
                      }
                      else {
                      return false;
                      }
                      }


                      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



                      function detectmob() {
                      if(window.innerWidth <= 800 && window.innerHeight <= 600) {
                      return true;
                      } else {
                      return false;
                      }
                      }


                      Reference:




                      • Detecting Browser and Devices with javascript






                      share|improve this answer















                      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.



                      window.mobilecheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };


                      For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:



                      window.mobileAndTabletcheck = function() {
                      var check = false;
                      (function(a){if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4))) check = true;})(navigator.userAgent||navigator.vendor||window.opera);
                      return check;
                      };




                      The Original Answer



                      You can do this by simply running through a list of devices and checking if the useragent matches anything like so:



                      function detectmob() { 
                      if( navigator.userAgent.match(/Android/i)
                      || navigator.userAgent.match(/webOS/i)
                      || navigator.userAgent.match(/iPhone/i)
                      || navigator.userAgent.match(/iPad/i)
                      || navigator.userAgent.match(/iPod/i)
                      || navigator.userAgent.match(/BlackBerry/i)
                      || navigator.userAgent.match(/Windows Phone/i)
                      ){
                      return true;
                      }
                      else {
                      return false;
                      }
                      }


                      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



                      function detectmob() {
                      if(window.innerWidth <= 800 && window.innerHeight <= 600) {
                      return true;
                      } else {
                      return false;
                      }
                      }


                      Reference:




                      • Detecting Browser and Devices with javascript







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 26 '16 at 9:26









                      Tiesselune

                      1,4071625




                      1,4071625










                      answered Jul 8 '12 at 8:24









                      Michael ZaporozhetsMichael Zaporozhets

                      15.2k22343




                      15.2k22343








                      • 20





                        Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

                        – Richard Lovejoy
                        Mar 27 '13 at 18:28






                      • 39





                        @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

                        – Michael Zaporozhets
                        Mar 28 '13 at 11:14






                      • 40





                        From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

                        – Gras Double
                        Jun 7 '13 at 0:28








                      • 11





                        Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

                        – molokoloco
                        Jun 26 '13 at 22:26






                      • 28





                        Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

                        – marksyzm
                        Aug 15 '13 at 10:07














                      • 20





                        Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

                        – Richard Lovejoy
                        Mar 27 '13 at 18:28






                      • 39





                        @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

                        – Michael Zaporozhets
                        Mar 28 '13 at 11:14






                      • 40





                        From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

                        – Gras Double
                        Jun 7 '13 at 0:28








                      • 11





                        Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

                        – molokoloco
                        Jun 26 '13 at 22:26






                      • 28





                        Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

                        – marksyzm
                        Aug 15 '13 at 10:07








                      20




                      20





                      Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

                      – Richard Lovejoy
                      Mar 27 '13 at 18:28





                      Hi I just visited the detectmobilebrowsers.com link on my iPad 3, iOS 6.1.2, and it says "No mobile browser detected".

                      – Richard Lovejoy
                      Mar 27 '13 at 18:28




                      39




                      39





                      @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

                      – Michael Zaporozhets
                      Mar 28 '13 at 11:14





                      @RichardLovejoy when building sites, the ipad is generally not considered a mobile.

                      – Michael Zaporozhets
                      Mar 28 '13 at 11:14




                      40




                      40





                      From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

                      – Gras Double
                      Jun 7 '13 at 0:28







                      From the about page: Android tablets, iPads, Kindle Fires and PlayBooks are not detected by design. To add support for tablets, add |android|ipad|playbook|silk to the first regex.

                      – Gras Double
                      Jun 7 '13 at 0:28






                      11




                      11





                      Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

                      – molokoloco
                      Jun 26 '13 at 22:26





                      Google TV is Android too. What define a mobile ? Screen Size ? Touch ? deviceOrientation ? When i design it's more a question of mousehover or not, big bouton or small links. So, for now, i run with "if (Modernizr.touch)" :)

                      – molokoloco
                      Jun 26 '13 at 22:26




                      28




                      28





                      Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

                      – marksyzm
                      Aug 15 '13 at 10:07





                      Gawd, this whole idea of user agents is awful and really, really needs to stop. We really need to stop allowing clients to fight against the tide and just stick with media queries. If they want to do redirects based on scale for particular pages, then just check the range of a particular media query via JS i.e. tylergaw.com/articles/reacting-to-media-queries-in-javascript

                      – marksyzm
                      Aug 15 '13 at 10:07













                      285














                      How about:



                      if (typeof window.orientation !== 'undefined') { ... }


                      ...since smartphones usually support this property and desktop browsers don't.



                      EDIT: As @Gajus pointed out, this solution is now deprecated and shouldn't be used (https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation)






                      share|improve this answer





















                      • 11





                        this is actually super unique and awesome, do you mind if I add it to my answer?

                        – Michael Zaporozhets
                        Jan 30 '13 at 0:34








                      • 73





                        This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

                        – Dave Hilditch
                        Feb 20 '13 at 17:33






                      • 9





                        as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

                        – yoav barnea
                        Mar 2 '13 at 20:01








                      • 2





                        Love it and works perfectly, thankyou. For my solution I was just after simple.

                        – Bojangles
                        Jun 18 '13 at 6:19






                      • 25





                        window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

                        – Gajus
                        Feb 8 '16 at 10:07
















                      285














                      How about:



                      if (typeof window.orientation !== 'undefined') { ... }


                      ...since smartphones usually support this property and desktop browsers don't.



                      EDIT: As @Gajus pointed out, this solution is now deprecated and shouldn't be used (https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation)






                      share|improve this answer





















                      • 11





                        this is actually super unique and awesome, do you mind if I add it to my answer?

                        – Michael Zaporozhets
                        Jan 30 '13 at 0:34








                      • 73





                        This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

                        – Dave Hilditch
                        Feb 20 '13 at 17:33






                      • 9





                        as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

                        – yoav barnea
                        Mar 2 '13 at 20:01








                      • 2





                        Love it and works perfectly, thankyou. For my solution I was just after simple.

                        – Bojangles
                        Jun 18 '13 at 6:19






                      • 25





                        window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

                        – Gajus
                        Feb 8 '16 at 10:07














                      285












                      285








                      285







                      How about:



                      if (typeof window.orientation !== 'undefined') { ... }


                      ...since smartphones usually support this property and desktop browsers don't.



                      EDIT: As @Gajus pointed out, this solution is now deprecated and shouldn't be used (https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation)






                      share|improve this answer















                      How about:



                      if (typeof window.orientation !== 'undefined') { ... }


                      ...since smartphones usually support this property and desktop browsers don't.



                      EDIT: As @Gajus pointed out, this solution is now deprecated and shouldn't be used (https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jan 18 at 23:30









                      flochtililoch

                      4,66221617




                      4,66221617










                      answered Jan 13 '13 at 8:24









                      yoav barneayoav barnea

                      4,32911525




                      4,32911525








                      • 11





                        this is actually super unique and awesome, do you mind if I add it to my answer?

                        – Michael Zaporozhets
                        Jan 30 '13 at 0:34








                      • 73





                        This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

                        – Dave Hilditch
                        Feb 20 '13 at 17:33






                      • 9





                        as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

                        – yoav barnea
                        Mar 2 '13 at 20:01








                      • 2





                        Love it and works perfectly, thankyou. For my solution I was just after simple.

                        – Bojangles
                        Jun 18 '13 at 6:19






                      • 25





                        window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

                        – Gajus
                        Feb 8 '16 at 10:07














                      • 11





                        this is actually super unique and awesome, do you mind if I add it to my answer?

                        – Michael Zaporozhets
                        Jan 30 '13 at 0:34








                      • 73





                        This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

                        – Dave Hilditch
                        Feb 20 '13 at 17:33






                      • 9





                        as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

                        – yoav barnea
                        Mar 2 '13 at 20:01








                      • 2





                        Love it and works perfectly, thankyou. For my solution I was just after simple.

                        – Bojangles
                        Jun 18 '13 at 6:19






                      • 25





                        window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

                        – Gajus
                        Feb 8 '16 at 10:07








                      11




                      11





                      this is actually super unique and awesome, do you mind if I add it to my answer?

                      – Michael Zaporozhets
                      Jan 30 '13 at 0:34







                      this is actually super unique and awesome, do you mind if I add it to my answer?

                      – Michael Zaporozhets
                      Jan 30 '13 at 0:34






                      73




                      73





                      This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

                      – Dave Hilditch
                      Feb 20 '13 at 17:33





                      This is probably not going to work for long. 1) Tablets can have decent screen sizes, you want them to display full desktop website but they will have this property 2) Windows 8 is here and with it a bunch of laptops with touch screen and screens that rotate.

                      – Dave Hilditch
                      Feb 20 '13 at 17:33




                      9




                      9





                      as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

                      – yoav barnea
                      Mar 2 '13 at 20:01







                      as for your first point about Tablets with decent screen sizes- I think you could make the same arguments for all others solutions as well (a tablet with big screen that is been tracked as small screen). anyway the idea here is to search for property that is been shared by small devices instead of keep maintence long code and add regex with every new coming device/vesion/model. I think device detection is belong to the past and today we need to focus on feature detection. again I will be glad to here about more suitable property for that matter...

                      – yoav barnea
                      Mar 2 '13 at 20:01






                      2




                      2





                      Love it and works perfectly, thankyou. For my solution I was just after simple.

                      – Bojangles
                      Jun 18 '13 at 6:19





                      Love it and works perfectly, thankyou. For my solution I was just after simple.

                      – Bojangles
                      Jun 18 '13 at 6:19




                      25




                      25





                      window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

                      – Gajus
                      Feb 8 '16 at 10:07





                      window.orientation is a deprecated property (developer.mozilla.org/en-US/docs/Web/API/Window/orientation, compat.spec.whatwg.org/#dom-window-orientation) that some mobile browsers choose to support for unknown reasons. Same browsers implement window.screen.orientation (which is defined in desktop browsers too). I can only assume that window.orientation is left there for legacy reasons and should therefore not be used in new applications.

                      – Gajus
                      Feb 8 '16 at 10:07











                      96














                      var isMobile = {
                      Android: function() {
                      return navigator.userAgent.match(/Android/i);
                      },
                      BlackBerry: function() {
                      return navigator.userAgent.match(/BlackBerry/i);
                      },
                      iOS: function() {
                      return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                      },
                      Opera: function() {
                      return navigator.userAgent.match(/Opera Mini/i);
                      },
                      Windows: function() {
                      return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
                      },
                      any: function() {
                      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                      }
                      };


                      How to use



                      if( isMobile.any() ) alert('Mobile');


                      To check to see if the user is on a specific mobile device:



                      if( isMobile.iOS() ) alert('iOS');


                      Ref: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript



                      Enhanced version on github : https://github.com/smali-kazmi/detect-mobile-browser






                      share|improve this answer


























                      • Why not make any() a for...in loop of ORed isMobile members?

                        – SomeShinyObject
                        Mar 1 '14 at 15:43











                      • @ChristopherW i had created its plugin and modified code as you advised

                        – Mudaser Ali
                        Apr 30 '14 at 19:57






                      • 1





                        Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

                        – Rob_vH
                        Mar 12 '15 at 14:33






                      • 2





                        @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

                        – Mudaser Ali
                        Mar 13 '15 at 19:45











                      • This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

                        – CSS
                        Aug 11 '15 at 18:11


















                      96














                      var isMobile = {
                      Android: function() {
                      return navigator.userAgent.match(/Android/i);
                      },
                      BlackBerry: function() {
                      return navigator.userAgent.match(/BlackBerry/i);
                      },
                      iOS: function() {
                      return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                      },
                      Opera: function() {
                      return navigator.userAgent.match(/Opera Mini/i);
                      },
                      Windows: function() {
                      return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
                      },
                      any: function() {
                      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                      }
                      };


                      How to use



                      if( isMobile.any() ) alert('Mobile');


                      To check to see if the user is on a specific mobile device:



                      if( isMobile.iOS() ) alert('iOS');


                      Ref: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript



                      Enhanced version on github : https://github.com/smali-kazmi/detect-mobile-browser






                      share|improve this answer


























                      • Why not make any() a for...in loop of ORed isMobile members?

                        – SomeShinyObject
                        Mar 1 '14 at 15:43











                      • @ChristopherW i had created its plugin and modified code as you advised

                        – Mudaser Ali
                        Apr 30 '14 at 19:57






                      • 1





                        Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

                        – Rob_vH
                        Mar 12 '15 at 14:33






                      • 2





                        @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

                        – Mudaser Ali
                        Mar 13 '15 at 19:45











                      • This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

                        – CSS
                        Aug 11 '15 at 18:11
















                      96












                      96








                      96







                      var isMobile = {
                      Android: function() {
                      return navigator.userAgent.match(/Android/i);
                      },
                      BlackBerry: function() {
                      return navigator.userAgent.match(/BlackBerry/i);
                      },
                      iOS: function() {
                      return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                      },
                      Opera: function() {
                      return navigator.userAgent.match(/Opera Mini/i);
                      },
                      Windows: function() {
                      return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
                      },
                      any: function() {
                      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                      }
                      };


                      How to use



                      if( isMobile.any() ) alert('Mobile');


                      To check to see if the user is on a specific mobile device:



                      if( isMobile.iOS() ) alert('iOS');


                      Ref: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript



                      Enhanced version on github : https://github.com/smali-kazmi/detect-mobile-browser






                      share|improve this answer















                      var isMobile = {
                      Android: function() {
                      return navigator.userAgent.match(/Android/i);
                      },
                      BlackBerry: function() {
                      return navigator.userAgent.match(/BlackBerry/i);
                      },
                      iOS: function() {
                      return navigator.userAgent.match(/iPhone|iPad|iPod/i);
                      },
                      Opera: function() {
                      return navigator.userAgent.match(/Opera Mini/i);
                      },
                      Windows: function() {
                      return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
                      },
                      any: function() {
                      return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
                      }
                      };


                      How to use



                      if( isMobile.any() ) alert('Mobile');


                      To check to see if the user is on a specific mobile device:



                      if( isMobile.iOS() ) alert('iOS');


                      Ref: http://www.abeautifulsite.net/blog/2011/11/detecting-mobile-devices-with-javascript



                      Enhanced version on github : https://github.com/smali-kazmi/detect-mobile-browser







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 27 '14 at 23:55

























                      answered Dec 11 '12 at 11:20









                      Mudaser AliMudaser Ali

                      2,5741720




                      2,5741720













                      • Why not make any() a for...in loop of ORed isMobile members?

                        – SomeShinyObject
                        Mar 1 '14 at 15:43











                      • @ChristopherW i had created its plugin and modified code as you advised

                        – Mudaser Ali
                        Apr 30 '14 at 19:57






                      • 1





                        Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

                        – Rob_vH
                        Mar 12 '15 at 14:33






                      • 2





                        @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

                        – Mudaser Ali
                        Mar 13 '15 at 19:45











                      • This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

                        – CSS
                        Aug 11 '15 at 18:11





















                      • Why not make any() a for...in loop of ORed isMobile members?

                        – SomeShinyObject
                        Mar 1 '14 at 15:43











                      • @ChristopherW i had created its plugin and modified code as you advised

                        – Mudaser Ali
                        Apr 30 '14 at 19:57






                      • 1





                        Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

                        – Rob_vH
                        Mar 12 '15 at 14:33






                      • 2





                        @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

                        – Mudaser Ali
                        Mar 13 '15 at 19:45











                      • This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

                        – CSS
                        Aug 11 '15 at 18:11



















                      Why not make any() a for...in loop of ORed isMobile members?

                      – SomeShinyObject
                      Mar 1 '14 at 15:43





                      Why not make any() a for...in loop of ORed isMobile members?

                      – SomeShinyObject
                      Mar 1 '14 at 15:43













                      @ChristopherW i had created its plugin and modified code as you advised

                      – Mudaser Ali
                      Apr 30 '14 at 19:57





                      @ChristopherW i had created its plugin and modified code as you advised

                      – Mudaser Ali
                      Apr 30 '14 at 19:57




                      1




                      1





                      Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

                      – Rob_vH
                      Mar 12 '15 at 14:33





                      Maybe move iOS ahead of BlackBerry() just to put the more common cases first and let early bailout save some extra processing?

                      – Rob_vH
                      Mar 12 '15 at 14:33




                      2




                      2





                      @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

                      – Mudaser Ali
                      Mar 13 '15 at 19:45





                      @Rob_vH i had put this code into github (github.com/smali-kazmi/detect-mobile-browser) with some advance features; you are open to send suggestions there as well

                      – Mudaser Ali
                      Mar 13 '15 at 19:45













                      This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

                      – CSS
                      Aug 11 '15 at 18:11







                      This one gets my upvote for content, but I'm trying to figure out how to convert it to John Papa's styling and having some difficulty. Still quite new to AngularJS as a whole (about a month into knowledge of it) and the vm. notation angles the learning curve upward a bit. Any help? -C§ EDIT: I'm trying to unit test it with karma-jasmine is why I ask.

                      – CSS
                      Aug 11 '15 at 18:11













                      43














                      Here is a simple solution from the source of facebook's slingshot



                      var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
                      if (isMobile) {
                      /* your code here */
                      }





                      share|improve this answer
























                      • Nice. Very useful in certain situations

                        – Chuck Le Butt
                        Mar 2 '18 at 20:16
















                      43














                      Here is a simple solution from the source of facebook's slingshot



                      var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
                      if (isMobile) {
                      /* your code here */
                      }





                      share|improve this answer
























                      • Nice. Very useful in certain situations

                        – Chuck Le Butt
                        Mar 2 '18 at 20:16














                      43












                      43








                      43







                      Here is a simple solution from the source of facebook's slingshot



                      var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
                      if (isMobile) {
                      /* your code here */
                      }





                      share|improve this answer













                      Here is a simple solution from the source of facebook's slingshot



                      var isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
                      if (isMobile) {
                      /* your code here */
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 8 '15 at 8:11









                      SanthoshSanthosh

                      2,4811910




                      2,4811910













                      • Nice. Very useful in certain situations

                        – Chuck Le Butt
                        Mar 2 '18 at 20:16



















                      • Nice. Very useful in certain situations

                        – Chuck Le Butt
                        Mar 2 '18 at 20:16

















                      Nice. Very useful in certain situations

                      – Chuck Le Butt
                      Mar 2 '18 at 20:16





                      Nice. Very useful in certain situations

                      – Chuck Le Butt
                      Mar 2 '18 at 20:16











                      16














                      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.



                      var touchDevice = ('ontouchstart' in document.documentElement);


                      Edit: To support desktops with a touch screen and mobiles at the same time you can use the following:



                      var touchDevice = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);





                      share|improve this answer





















                      • 6





                        What if desktop's monitor supports touch?

                        – Anton Kuzmin
                        Aug 1 '16 at 1:25











                      • @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

                        – Tigger
                        Aug 1 '16 at 9:09











                      • (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

                        – Cody
                        Dec 16 '16 at 21:18






                      • 1





                        Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

                        – J.T. Taylor
                        Jul 15 '17 at 0:31






                      • 1





                        @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

                        – Tigger
                        Jul 15 '17 at 0:43
















                      16














                      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.



                      var touchDevice = ('ontouchstart' in document.documentElement);


                      Edit: To support desktops with a touch screen and mobiles at the same time you can use the following:



                      var touchDevice = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);





                      share|improve this answer





















                      • 6





                        What if desktop's monitor supports touch?

                        – Anton Kuzmin
                        Aug 1 '16 at 1:25











                      • @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

                        – Tigger
                        Aug 1 '16 at 9:09











                      • (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

                        – Cody
                        Dec 16 '16 at 21:18






                      • 1





                        Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

                        – J.T. Taylor
                        Jul 15 '17 at 0:31






                      • 1





                        @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

                        – Tigger
                        Jul 15 '17 at 0:43














                      16












                      16








                      16







                      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.



                      var touchDevice = ('ontouchstart' in document.documentElement);


                      Edit: To support desktops with a touch screen and mobiles at the same time you can use the following:



                      var touchDevice = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);





                      share|improve this answer















                      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.



                      var touchDevice = ('ontouchstart' in document.documentElement);


                      Edit: To support desktops with a touch screen and mobiles at the same time you can use the following:



                      var touchDevice = (navigator.maxTouchPoints || 'ontouchstart' in document.documentElement);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 28 '17 at 9:01

























                      answered Apr 17 '16 at 5:58









                      TiggerTigger

                      6,21932633




                      6,21932633








                      • 6





                        What if desktop's monitor supports touch?

                        – Anton Kuzmin
                        Aug 1 '16 at 1:25











                      • @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

                        – Tigger
                        Aug 1 '16 at 9:09











                      • (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

                        – Cody
                        Dec 16 '16 at 21:18






                      • 1





                        Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

                        – J.T. Taylor
                        Jul 15 '17 at 0:31






                      • 1





                        @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

                        – Tigger
                        Jul 15 '17 at 0:43














                      • 6





                        What if desktop's monitor supports touch?

                        – Anton Kuzmin
                        Aug 1 '16 at 1:25











                      • @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

                        – Tigger
                        Aug 1 '16 at 9:09











                      • (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

                        – Cody
                        Dec 16 '16 at 21:18






                      • 1





                        Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

                        – J.T. Taylor
                        Jul 15 '17 at 0:31






                      • 1





                        @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

                        – Tigger
                        Jul 15 '17 at 0:43








                      6




                      6





                      What if desktop's monitor supports touch?

                      – Anton Kuzmin
                      Aug 1 '16 at 1:25





                      What if desktop's monitor supports touch?

                      – Anton Kuzmin
                      Aug 1 '16 at 1:25













                      @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

                      – Tigger
                      Aug 1 '16 at 9:09





                      @HappyCoder I believe it is up to the OS to tell the browser when the touch screen on a desktop is active. So, yes this check should still be valid.

                      – Tigger
                      Aug 1 '16 at 9:09













                      (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

                      – Cody
                      Dec 16 '16 at 21:18





                      (+1), however, my desktop I'm using now has a touchScreen, and it isn't always consistent for touchstart|end|etc.

                      – Cody
                      Dec 16 '16 at 21:18




                      1




                      1





                      Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

                      – J.T. Taylor
                      Jul 15 '17 at 0:31





                      Bootstrap datepicker uses the following: if ( window.navigator.msMaxTouchPoints || 'ontouchstart' in document) { this.input.blur(); }

                      – J.T. Taylor
                      Jul 15 '17 at 0:31




                      1




                      1





                      @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

                      – Tigger
                      Jul 15 '17 at 0:43





                      @J.T.Taylor It looks like Microsoft is recommending navigator.maxTouchPoints (no ms prefix). There is also an MDN article to check.

                      – Tigger
                      Jul 15 '17 at 0:43











                      13














                      There's no perfect solution for detecting whether JS code is executed on a mobile browser, but the following two options should work in most cases.



                      Option 1 : browser sniffing






                      !function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

                      alert(isMobile.any ? 'Mobile' : 'Not mobile');





                      This particular browser sniffing code is that of a library called isMobile.





                      Option 2 : window.orientation



                      Test if window.orientation is defined :






                      var isMobile = window.orientation > -1;
                      alert(isMobile ? 'Mobile' : 'Not mobile');







                      Note



                      Not all touchscreen devices are mobile and vice versa. So, if you want to implement something specifically for touchscreen, you shouldn't test if your browser is run on a mobile device but rather whether the devices has touchscreen support :






                      var hasTouchscreen = 'ontouchstart' in window;
                      alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');








                      share|improve this answer


























                      • Orientation approach is really nice! ))

                        – Maxim
                        Oct 19 '17 at 11:57











                      • I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

                        – skwidbreth
                        Apr 10 '18 at 17:58






                      • 2





                        Orientation approach is NOT nice because Windows 8 and higher can change orientation.

                        – Heitor
                        Aug 4 '18 at 6:58
















                      13














                      There's no perfect solution for detecting whether JS code is executed on a mobile browser, but the following two options should work in most cases.



                      Option 1 : browser sniffing






                      !function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

                      alert(isMobile.any ? 'Mobile' : 'Not mobile');





                      This particular browser sniffing code is that of a library called isMobile.





                      Option 2 : window.orientation



                      Test if window.orientation is defined :






                      var isMobile = window.orientation > -1;
                      alert(isMobile ? 'Mobile' : 'Not mobile');







                      Note



                      Not all touchscreen devices are mobile and vice versa. So, if you want to implement something specifically for touchscreen, you shouldn't test if your browser is run on a mobile device but rather whether the devices has touchscreen support :






                      var hasTouchscreen = 'ontouchstart' in window;
                      alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');








                      share|improve this answer


























                      • Orientation approach is really nice! ))

                        – Maxim
                        Oct 19 '17 at 11:57











                      • I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

                        – skwidbreth
                        Apr 10 '18 at 17:58






                      • 2





                        Orientation approach is NOT nice because Windows 8 and higher can change orientation.

                        – Heitor
                        Aug 4 '18 at 6:58














                      13












                      13








                      13







                      There's no perfect solution for detecting whether JS code is executed on a mobile browser, but the following two options should work in most cases.



                      Option 1 : browser sniffing






                      !function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

                      alert(isMobile.any ? 'Mobile' : 'Not mobile');





                      This particular browser sniffing code is that of a library called isMobile.





                      Option 2 : window.orientation



                      Test if window.orientation is defined :






                      var isMobile = window.orientation > -1;
                      alert(isMobile ? 'Mobile' : 'Not mobile');







                      Note



                      Not all touchscreen devices are mobile and vice versa. So, if you want to implement something specifically for touchscreen, you shouldn't test if your browser is run on a mobile device but rather whether the devices has touchscreen support :






                      var hasTouchscreen = 'ontouchstart' in window;
                      alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');








                      share|improve this answer















                      There's no perfect solution for detecting whether JS code is executed on a mobile browser, but the following two options should work in most cases.



                      Option 1 : browser sniffing






                      !function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

                      alert(isMobile.any ? 'Mobile' : 'Not mobile');





                      This particular browser sniffing code is that of a library called isMobile.





                      Option 2 : window.orientation



                      Test if window.orientation is defined :






                      var isMobile = window.orientation > -1;
                      alert(isMobile ? 'Mobile' : 'Not mobile');







                      Note



                      Not all touchscreen devices are mobile and vice versa. So, if you want to implement something specifically for touchscreen, you shouldn't test if your browser is run on a mobile device but rather whether the devices has touchscreen support :






                      var hasTouchscreen = 'ontouchstart' in window;
                      alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');








                      !function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

                      alert(isMobile.any ? 'Mobile' : 'Not mobile');





                      !function(a){var b=/iPhone/i,c=/iPod/i,d=/iPad/i,e=/(?=.*bAndroidb)(?=.*bMobileb)/i,f=/Android/i,g=/(?=.*bAndroidb)(?=.*bSD4930URb)/i,h=/(?=.*bAndroidb)(?=.*b(?:KFOT|KFTT|KFJWI|KFJWA|KFSOWI|KFTHWI|KFTHWA|KFAPWI|KFAPWA|KFARWI|KFASWI|KFSAWI|KFSAWA)b)/i,i=/IEMobile/i,j=/(?=.*bWindowsb)(?=.*bARMb)/i,k=/BlackBerry/i,l=/BB10/i,m=/Opera Mini/i,n=/(CriOS|Chrome)(?=.*bMobileb)/i,o=/(?=.*bFirefoxb)(?=.*bMobileb)/i,p=new RegExp("(?:Nexus 7|BNTV250|Kindle Fire|Silk|GT-P1000)","i"),q=function(a,b){return a.test(b)},r=function(a){var r=a||navigator.userAgent,s=r.split("[FBAN");return"undefined"!=typeof s[1]&&(r=s[0]),s=r.split("Twitter"),"undefined"!=typeof s[1]&&(r=s[0]),this.apple={phone:q(b,r),ipod:q(c,r),tablet:!q(b,r)&&q(d,r),device:q(b,r)||q(c,r)||q(d,r)},this.amazon={phone:q(g,r),tablet:!q(g,r)&&q(h,r),device:q(g,r)||q(h,r)},this.android={phone:q(g,r)||q(e,r),tablet:!q(g,r)&&!q(e,r)&&(q(h,r)||q(f,r)),device:q(g,r)||q(h,r)||q(e,r)||q(f,r)},this.windows={phone:q(i,r),tablet:q(j,r),device:q(i,r)||q(j,r)},this.other={blackberry:q(k,r),blackberry10:q(l,r),opera:q(m,r),firefox:q(o,r),chrome:q(n,r),device:q(k,r)||q(l,r)||q(m,r)||q(o,r)||q(n,r)},this.seven_inch=q(p,r),this.any=this.apple.device||this.android.device||this.windows.device||this.other.device||this.seven_inch,this.phone=this.apple.phone||this.android.phone||this.windows.phone,this.tablet=this.apple.tablet||this.android.tablet||this.windows.tablet,"undefined"==typeof window?this:void 0},s=function(){var a=new r;return a.Class=r,a};"undefined"!=typeof module&&module.exports&&"undefined"==typeof window?module.exports=r:"undefined"!=typeof module&&module.exports&&"undefined"!=typeof window?module.exports=s():"function"==typeof define&&define.amd?define("isMobile",,a.isMobile=s()):a.isMobile=s()}(this);

                      alert(isMobile.any ? 'Mobile' : 'Not mobile');





                      var isMobile = window.orientation > -1;
                      alert(isMobile ? 'Mobile' : 'Not mobile');





                      var isMobile = window.orientation > -1;
                      alert(isMobile ? 'Mobile' : 'Not mobile');





                      var hasTouchscreen = 'ontouchstart' in window;
                      alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');





                      var hasTouchscreen = 'ontouchstart' in window;
                      alert(hasTouchscreen ? 'has touchscreen' : 'doesn't have touchscreen');






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Oct 20 '17 at 10:46

























                      answered Mar 12 '16 at 2:31









                      John SlegersJohn Slegers

                      27.8k13148129




                      27.8k13148129













                      • Orientation approach is really nice! ))

                        – Maxim
                        Oct 19 '17 at 11:57











                      • I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

                        – skwidbreth
                        Apr 10 '18 at 17:58






                      • 2





                        Orientation approach is NOT nice because Windows 8 and higher can change orientation.

                        – Heitor
                        Aug 4 '18 at 6:58



















                      • Orientation approach is really nice! ))

                        – Maxim
                        Oct 19 '17 at 11:57











                      • I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

                        – skwidbreth
                        Apr 10 '18 at 17:58






                      • 2





                        Orientation approach is NOT nice because Windows 8 and higher can change orientation.

                        – Heitor
                        Aug 4 '18 at 6:58

















                      Orientation approach is really nice! ))

                      – Maxim
                      Oct 19 '17 at 11:57





                      Orientation approach is really nice! ))

                      – Maxim
                      Oct 19 '17 at 11:57













                      I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

                      – skwidbreth
                      Apr 10 '18 at 17:58





                      I like your window.orientation solution, but the docs say it's deprecated! developer.mozilla.org/en-US/docs/Web/API/Window/orientation

                      – skwidbreth
                      Apr 10 '18 at 17:58




                      2




                      2





                      Orientation approach is NOT nice because Windows 8 and higher can change orientation.

                      – Heitor
                      Aug 4 '18 at 6:58





                      Orientation approach is NOT nice because Windows 8 and higher can change orientation.

                      – Heitor
                      Aug 4 '18 at 6:58











                      12














                      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:



                      // Used to detect whether the users browser is an mobile browser
                      function isMobile() {
                      ///<summary>Detecting whether the browser is a mobile browser or desktop browser</summary>
                      ///<returns>A boolean value indicating whether the browser is a mobile browser or not</returns>

                      if (sessionStorage.desktop) // desktop storage
                      return false;
                      else if (localStorage.mobile) // mobile storage
                      return true;

                      // alternative
                      var mobile = ['iphone','ipad','android','blackberry','nokia','opera mini','windows mobile','windows phone','iemobile'];
                      for (var i in mobile) if (navigator.userAgent.toLowerCase().indexOf(mobile[i].toLowerCase()) > 0) return true;

                      // nothing found.. assume desktop
                      return false;
                      }





                      share|improve this answer





















                      • 1





                        Can I please have a reason for the downvote?

                        – Rasmus Søborg
                        Jun 27 '13 at 12:46






                      • 3





                        I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

                        – shuckster
                        Dec 28 '13 at 15:33








                      • 3





                        Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

                        – Jeffrey Gilbert
                        Jun 2 '14 at 22:19
















                      12














                      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:



                      // Used to detect whether the users browser is an mobile browser
                      function isMobile() {
                      ///<summary>Detecting whether the browser is a mobile browser or desktop browser</summary>
                      ///<returns>A boolean value indicating whether the browser is a mobile browser or not</returns>

                      if (sessionStorage.desktop) // desktop storage
                      return false;
                      else if (localStorage.mobile) // mobile storage
                      return true;

                      // alternative
                      var mobile = ['iphone','ipad','android','blackberry','nokia','opera mini','windows mobile','windows phone','iemobile'];
                      for (var i in mobile) if (navigator.userAgent.toLowerCase().indexOf(mobile[i].toLowerCase()) > 0) return true;

                      // nothing found.. assume desktop
                      return false;
                      }





                      share|improve this answer





















                      • 1





                        Can I please have a reason for the downvote?

                        – Rasmus Søborg
                        Jun 27 '13 at 12:46






                      • 3





                        I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

                        – shuckster
                        Dec 28 '13 at 15:33








                      • 3





                        Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

                        – Jeffrey Gilbert
                        Jun 2 '14 at 22:19














                      12












                      12








                      12







                      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:



                      // Used to detect whether the users browser is an mobile browser
                      function isMobile() {
                      ///<summary>Detecting whether the browser is a mobile browser or desktop browser</summary>
                      ///<returns>A boolean value indicating whether the browser is a mobile browser or not</returns>

                      if (sessionStorage.desktop) // desktop storage
                      return false;
                      else if (localStorage.mobile) // mobile storage
                      return true;

                      // alternative
                      var mobile = ['iphone','ipad','android','blackberry','nokia','opera mini','windows mobile','windows phone','iemobile'];
                      for (var i in mobile) if (navigator.userAgent.toLowerCase().indexOf(mobile[i].toLowerCase()) > 0) return true;

                      // nothing found.. assume desktop
                      return false;
                      }





                      share|improve this answer















                      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:



                      // Used to detect whether the users browser is an mobile browser
                      function isMobile() {
                      ///<summary>Detecting whether the browser is a mobile browser or desktop browser</summary>
                      ///<returns>A boolean value indicating whether the browser is a mobile browser or not</returns>

                      if (sessionStorage.desktop) // desktop storage
                      return false;
                      else if (localStorage.mobile) // mobile storage
                      return true;

                      // alternative
                      var mobile = ['iphone','ipad','android','blackberry','nokia','opera mini','windows mobile','windows phone','iemobile'];
                      for (var i in mobile) if (navigator.userAgent.toLowerCase().indexOf(mobile[i].toLowerCase()) > 0) return true;

                      // nothing found.. assume desktop
                      return false;
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 28 '13 at 22:28

























                      answered Apr 18 '13 at 10:17









                      Rasmus SøborgRasmus Søborg

                      1,86832339




                      1,86832339








                      • 1





                        Can I please have a reason for the downvote?

                        – Rasmus Søborg
                        Jun 27 '13 at 12:46






                      • 3





                        I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

                        – shuckster
                        Dec 28 '13 at 15:33








                      • 3





                        Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

                        – Jeffrey Gilbert
                        Jun 2 '14 at 22:19














                      • 1





                        Can I please have a reason for the downvote?

                        – Rasmus Søborg
                        Jun 27 '13 at 12:46






                      • 3





                        I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

                        – shuckster
                        Dec 28 '13 at 15:33








                      • 3





                        Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

                        – Jeffrey Gilbert
                        Jun 2 '14 at 22:19








                      1




                      1





                      Can I please have a reason for the downvote?

                      – Rasmus Søborg
                      Jun 27 '13 at 12:46





                      Can I please have a reason for the downvote?

                      – Rasmus Søborg
                      Jun 27 '13 at 12:46




                      3




                      3





                      I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

                      – shuckster
                      Dec 28 '13 at 15:33







                      I haven't tested on mobile yet, but sessionStorage.desktop doesn't exist in either Safari, Chrome, or Firefox (all newest versions at time of post). You get an up-vote though, since your solution goes in a better direction than others. But don't forget to use var mobile = instead of mobile =.

                      – shuckster
                      Dec 28 '13 at 15:33






                      3




                      3





                      Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

                      – Jeffrey Gilbert
                      Jun 2 '14 at 22:19





                      Also a good idea not to use indexOf with older browsers still being around which don't support that method, or to use a polyfill. It's not necessary to use toLowerCase on a list of lowercase values, nor is it necessary to do so if you're running /ipad|iphone|etc/i.test(navigator.userAgent) instead of the slow loop you have up there.

                      – Jeffrey Gilbert
                      Jun 2 '14 at 22:19











                      10














                      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.






                      share|improve this answer



















                      • 14





                        You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

                        – Jonathan Allard
                        Jun 1 '16 at 19:10






                      • 1





                        This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

                        – twicejr
                        Aug 4 '17 at 14:24
















                      10














                      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.






                      share|improve this answer



















                      • 14





                        You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

                        – Jonathan Allard
                        Jun 1 '16 at 19:10






                      • 1





                        This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

                        – twicejr
                        Aug 4 '17 at 14:24














                      10












                      10








                      10







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 5 '13 at 0:48









                      AbuzzyAbuzzy

                      45621230




                      45621230








                      • 14





                        You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

                        – Jonathan Allard
                        Jun 1 '16 at 19:10






                      • 1





                        This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

                        – twicejr
                        Aug 4 '17 at 14:24














                      • 14





                        You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

                        – Jonathan Allard
                        Jun 1 '16 at 19:10






                      • 1





                        This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

                        – twicejr
                        Aug 4 '17 at 14:24








                      14




                      14





                      You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

                      – Jonathan Allard
                      Jun 1 '16 at 19:10





                      You've answered another question than what was asked. Rather than "how can I detect mobile?", you've answered "how can I detect certain features?". Not all device detection is for feature detection. What if we were looking to get statistics about devices? Then no, "feature detection" is not "much better than [figuring out device]".

                      – Jonathan Allard
                      Jun 1 '16 at 19:10




                      1




                      1





                      This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

                      – twicejr
                      Aug 4 '17 at 14:24





                      This is not the answer, but it deserves more than just a comment. The question is: why do you want to detect a browser and then you will probably want to know it because of the (lack of) touch only. Responsive webdesign suffices in most if not all cases.

                      – twicejr
                      Aug 4 '17 at 14:24











                      10














                      How about something like this?



                      if(
                      (screen.width <= 640) ||
                      (window.matchMedia &&
                      window.matchMedia('only screen and (max-width: 640px)').matches
                      )
                      ){
                      // Do the mobile thing
                      }





                      share|improve this answer
























                      • Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

                        – John Slegers
                        Mar 12 '16 at 2:30











                      • Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

                        – stujo
                        Mar 23 '16 at 23:35











                      • Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

                        – John Slegers
                        Mar 24 '16 at 7:33








                      • 1





                        Thanks John :) Yeay Progress!

                        – stujo
                        Apr 12 '16 at 3:45






                      • 2





                        Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

                        – ʇsәɹoɈ
                        Apr 21 '16 at 2:16


















                      10














                      How about something like this?



                      if(
                      (screen.width <= 640) ||
                      (window.matchMedia &&
                      window.matchMedia('only screen and (max-width: 640px)').matches
                      )
                      ){
                      // Do the mobile thing
                      }





                      share|improve this answer
























                      • Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

                        – John Slegers
                        Mar 12 '16 at 2:30











                      • Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

                        – stujo
                        Mar 23 '16 at 23:35











                      • Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

                        – John Slegers
                        Mar 24 '16 at 7:33








                      • 1





                        Thanks John :) Yeay Progress!

                        – stujo
                        Apr 12 '16 at 3:45






                      • 2





                        Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

                        – ʇsәɹoɈ
                        Apr 21 '16 at 2:16
















                      10












                      10








                      10







                      How about something like this?



                      if(
                      (screen.width <= 640) ||
                      (window.matchMedia &&
                      window.matchMedia('only screen and (max-width: 640px)').matches
                      )
                      ){
                      // Do the mobile thing
                      }





                      share|improve this answer













                      How about something like this?



                      if(
                      (screen.width <= 640) ||
                      (window.matchMedia &&
                      window.matchMedia('only screen and (max-width: 640px)').matches
                      )
                      ){
                      // Do the mobile thing
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Apr 24 '15 at 2:34









                      stujostujo

                      1,3551125




                      1,3551125













                      • Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

                        – John Slegers
                        Mar 12 '16 at 2:30











                      • Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

                        – stujo
                        Mar 23 '16 at 23:35











                      • Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

                        – John Slegers
                        Mar 24 '16 at 7:33








                      • 1





                        Thanks John :) Yeay Progress!

                        – stujo
                        Apr 12 '16 at 3:45






                      • 2





                        Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

                        – ʇsәɹoɈ
                        Apr 21 '16 at 2:16





















                      • Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

                        – John Slegers
                        Mar 12 '16 at 2:30











                      • Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

                        – stujo
                        Mar 23 '16 at 23:35











                      • Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

                        – John Slegers
                        Mar 24 '16 at 7:33








                      • 1





                        Thanks John :) Yeay Progress!

                        – stujo
                        Apr 12 '16 at 3:45






                      • 2





                        Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

                        – ʇsәɹoɈ
                        Apr 21 '16 at 2:16



















                      Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

                      – John Slegers
                      Mar 12 '16 at 2:30





                      Why not just use screen.width instead? It seems to me that's more reliable than window.matchMedia.

                      – John Slegers
                      Mar 12 '16 at 2:30













                      Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

                      – stujo
                      Mar 23 '16 at 23:35





                      Good point John, I can't recall exactly what I was thinking at the time, it seems unlikely (looking at it now) that the second clause would return true if the first is false. There must have been some reason I added it though.

                      – stujo
                      Mar 23 '16 at 23:35













                      Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

                      – John Slegers
                      Mar 24 '16 at 7:33







                      Most decent programmers feel ashamed when they see code they wrote themselves a year earlier. Those who don't just haven't grown as programmers ;-)

                      – John Slegers
                      Mar 24 '16 at 7:33






                      1




                      1





                      Thanks John :) Yeay Progress!

                      – stujo
                      Apr 12 '16 at 3:45





                      Thanks John :) Yeay Progress!

                      – stujo
                      Apr 12 '16 at 3:45




                      2




                      2





                      Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

                      – ʇsәɹoɈ
                      Apr 21 '16 at 2:16







                      Window resolution has nothing to do with whether a browser is on a mobile device or not. For example, lots of desktop browsers run in non-full-screen windows. If you present a UI designed for handheld screens to those browsers, their users are going to have a frustrating experience.

                      – ʇsәɹoɈ
                      Apr 21 '16 at 2:16













                      8














                      Here is a userAgent solution that is more efficent than match...



                      function _isMobile(){
                      // if we want a more complete list use this: http://detectmobilebrowsers.com/
                      // str.test() is more efficent than str.match()
                      // remember str.test is case sensitive
                      var isMobile = (/iphone|ipod|android|ie|blackberry|fennec/).test
                      (navigator.userAgent.toLowerCase());
                      return isMobile;
                      }





                      share|improve this answer



















                      • 7





                        the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

                        – xec
                        Feb 5 '13 at 17:01


















                      8














                      Here is a userAgent solution that is more efficent than match...



                      function _isMobile(){
                      // if we want a more complete list use this: http://detectmobilebrowsers.com/
                      // str.test() is more efficent than str.match()
                      // remember str.test is case sensitive
                      var isMobile = (/iphone|ipod|android|ie|blackberry|fennec/).test
                      (navigator.userAgent.toLowerCase());
                      return isMobile;
                      }





                      share|improve this answer



















                      • 7





                        the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

                        – xec
                        Feb 5 '13 at 17:01
















                      8












                      8








                      8







                      Here is a userAgent solution that is more efficent than match...



                      function _isMobile(){
                      // if we want a more complete list use this: http://detectmobilebrowsers.com/
                      // str.test() is more efficent than str.match()
                      // remember str.test is case sensitive
                      var isMobile = (/iphone|ipod|android|ie|blackberry|fennec/).test
                      (navigator.userAgent.toLowerCase());
                      return isMobile;
                      }





                      share|improve this answer













                      Here is a userAgent solution that is more efficent than match...



                      function _isMobile(){
                      // if we want a more complete list use this: http://detectmobilebrowsers.com/
                      // str.test() is more efficent than str.match()
                      // remember str.test is case sensitive
                      var isMobile = (/iphone|ipod|android|ie|blackberry|fennec/).test
                      (navigator.userAgent.toLowerCase());
                      return isMobile;
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 4 '12 at 16:19









                      JeffJakJeffJak

                      86531938




                      86531938








                      • 7





                        the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

                        – xec
                        Feb 5 '13 at 17:01
















                      • 7





                        the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

                        – xec
                        Feb 5 '13 at 17:01










                      7




                      7





                      the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

                      – xec
                      Feb 5 '13 at 17:01







                      the test method is not case sensitive, but your regex is. you could just flag for case insensitive regex with an "i" at the end and do /iphone|etc/i.test(navigator.userAgent)

                      – xec
                      Feb 5 '13 at 17:01













                      6














                      A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.



                      Plain JavaScript Code:



                      function isMobile() {
                      try{ document.createEvent("TouchEvent"); return true; }
                      catch(e){ return false; }
                      }

                      if (isMobile()) {
                      # do whatever you wanna do!
                      }


                      This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.



                      I am not sure if a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.






                      share|improve this answer





















                      • 3





                        Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

                        – blissfool
                        Jun 4 '18 at 20:59













                      • @blissfool i guess this will not be the right way for detecting mobile devices then.

                        – Neo Morina
                        Jun 12 '18 at 7:08













                      • Unfortunately, no. But, it might still be a viable option for a very limited use case.

                        – blissfool
                        Jun 13 '18 at 14:51











                      • never write code, that is based on an exception, that will be throwen for sure in any case...

                        – Sivic
                        Nov 15 '18 at 8:30
















                      6














                      A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.



                      Plain JavaScript Code:



                      function isMobile() {
                      try{ document.createEvent("TouchEvent"); return true; }
                      catch(e){ return false; }
                      }

                      if (isMobile()) {
                      # do whatever you wanna do!
                      }


                      This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.



                      I am not sure if a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.






                      share|improve this answer





















                      • 3





                        Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

                        – blissfool
                        Jun 4 '18 at 20:59













                      • @blissfool i guess this will not be the right way for detecting mobile devices then.

                        – Neo Morina
                        Jun 12 '18 at 7:08













                      • Unfortunately, no. But, it might still be a viable option for a very limited use case.

                        – blissfool
                        Jun 13 '18 at 14:51











                      • never write code, that is based on an exception, that will be throwen for sure in any case...

                        – Sivic
                        Nov 15 '18 at 8:30














                      6












                      6








                      6







                      A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.



                      Plain JavaScript Code:



                      function isMobile() {
                      try{ document.createEvent("TouchEvent"); return true; }
                      catch(e){ return false; }
                      }

                      if (isMobile()) {
                      # do whatever you wanna do!
                      }


                      This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.



                      I am not sure if a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.






                      share|improve this answer















                      A really good way of detecting mobile or tablet devices is by looking if the browser can create a touch event.



                      Plain JavaScript Code:



                      function isMobile() {
                      try{ document.createEvent("TouchEvent"); return true; }
                      catch(e){ return false; }
                      }

                      if (isMobile()) {
                      # do whatever you wanna do!
                      }


                      This worked for me really well, but there may be a problem with laptop devices which include a touchscreen display.



                      I am not sure if a touchscreen Laptop will get detected as a mobile device because I haven't tested it yet.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 26 '18 at 12:36

























                      answered Mar 22 '18 at 10:12









                      Neo MorinaNeo Morina

                      169110




                      169110








                      • 3





                        Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

                        – blissfool
                        Jun 4 '18 at 20:59













                      • @blissfool i guess this will not be the right way for detecting mobile devices then.

                        – Neo Morina
                        Jun 12 '18 at 7:08













                      • Unfortunately, no. But, it might still be a viable option for a very limited use case.

                        – blissfool
                        Jun 13 '18 at 14:51











                      • never write code, that is based on an exception, that will be throwen for sure in any case...

                        – Sivic
                        Nov 15 '18 at 8:30














                      • 3





                        Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

                        – blissfool
                        Jun 4 '18 at 20:59













                      • @blissfool i guess this will not be the right way for detecting mobile devices then.

                        – Neo Morina
                        Jun 12 '18 at 7:08













                      • Unfortunately, no. But, it might still be a viable option for a very limited use case.

                        – blissfool
                        Jun 13 '18 at 14:51











                      • never write code, that is based on an exception, that will be throwen for sure in any case...

                        – Sivic
                        Nov 15 '18 at 8:30








                      3




                      3





                      Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

                      – blissfool
                      Jun 4 '18 at 20:59







                      Touch screen laptops will be detected as mobile device. As well as touch screen monitors for desktops. Believe it or not, you will also run into issue if you are using touchscreen device to RDP into another device that is does not have a touch screen.

                      – blissfool
                      Jun 4 '18 at 20:59















                      @blissfool i guess this will not be the right way for detecting mobile devices then.

                      – Neo Morina
                      Jun 12 '18 at 7:08







                      @blissfool i guess this will not be the right way for detecting mobile devices then.

                      – Neo Morina
                      Jun 12 '18 at 7:08















                      Unfortunately, no. But, it might still be a viable option for a very limited use case.

                      – blissfool
                      Jun 13 '18 at 14:51





                      Unfortunately, no. But, it might still be a viable option for a very limited use case.

                      – blissfool
                      Jun 13 '18 at 14:51













                      never write code, that is based on an exception, that will be throwen for sure in any case...

                      – Sivic
                      Nov 15 '18 at 8:30





                      never write code, that is based on an exception, that will be throwen for sure in any case...

                      – Sivic
                      Nov 15 '18 at 8:30











                      5














                      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.



                      window.onload = userAgentDetect;
                      function userAgentDetect() {
                      if(window.navigator.userAgent.match(/Mobile/i)
                      || window.navigator.userAgent.match(/iPhone/i)
                      || window.navigator.userAgent.match(/iPod/i)
                      || window.navigator.userAgent.match(/IEMobile/i)
                      || window.navigator.userAgent.match(/Windows Phone/i)
                      || window.navigator.userAgent.match(/Android/i)
                      || window.navigator.userAgent.match(/BlackBerry/i)
                      || window.navigator.userAgent.match(/webOS/i)) {
                      document.body.className += ' mobile';
                      alert('True - Mobile - ' + navigator.userAgent);
                      } else {
                      alert('False - Mobile - ' + navigator.userAgent);
                      }
                      if(window.navigator.userAgent.match(/Tablet/i)
                      || window.navigator.userAgent.match(/iPad/i)
                      || window.navigator.userAgent.match(/Nexus 7/i)
                      || window.navigator.userAgent.match(/Nexus 10/i)
                      || window.navigator.userAgent.match(/KFAPWI/i)) {
                      document.body.className -= ' mobile';
                      document.body.className += ' tablet';
                      alert('True - Tablet - ' + navigator.userAgent);
                      } else {
                      alert('False - Tablet - ' + navigator.userAgent);
                      }
                      }


                      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:



                      body.tablet { background-color: green; }
                      body.mobile { background-color: red; }


                      alert lines added for development. Chrome console can emulate many handheld devices. Test there.



                      EDIT:



                      Just don't use this, use feature detection instead. There are so many devices and brands out there that targeting a brand NEVER will be the right solution.






                      share|improve this answer






























                        5














                        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.



                        window.onload = userAgentDetect;
                        function userAgentDetect() {
                        if(window.navigator.userAgent.match(/Mobile/i)
                        || window.navigator.userAgent.match(/iPhone/i)
                        || window.navigator.userAgent.match(/iPod/i)
                        || window.navigator.userAgent.match(/IEMobile/i)
                        || window.navigator.userAgent.match(/Windows Phone/i)
                        || window.navigator.userAgent.match(/Android/i)
                        || window.navigator.userAgent.match(/BlackBerry/i)
                        || window.navigator.userAgent.match(/webOS/i)) {
                        document.body.className += ' mobile';
                        alert('True - Mobile - ' + navigator.userAgent);
                        } else {
                        alert('False - Mobile - ' + navigator.userAgent);
                        }
                        if(window.navigator.userAgent.match(/Tablet/i)
                        || window.navigator.userAgent.match(/iPad/i)
                        || window.navigator.userAgent.match(/Nexus 7/i)
                        || window.navigator.userAgent.match(/Nexus 10/i)
                        || window.navigator.userAgent.match(/KFAPWI/i)) {
                        document.body.className -= ' mobile';
                        document.body.className += ' tablet';
                        alert('True - Tablet - ' + navigator.userAgent);
                        } else {
                        alert('False - Tablet - ' + navigator.userAgent);
                        }
                        }


                        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:



                        body.tablet { background-color: green; }
                        body.mobile { background-color: red; }


                        alert lines added for development. Chrome console can emulate many handheld devices. Test there.



                        EDIT:



                        Just don't use this, use feature detection instead. There are so many devices and brands out there that targeting a brand NEVER will be the right solution.






                        share|improve this answer




























                          5












                          5








                          5







                          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.



                          window.onload = userAgentDetect;
                          function userAgentDetect() {
                          if(window.navigator.userAgent.match(/Mobile/i)
                          || window.navigator.userAgent.match(/iPhone/i)
                          || window.navigator.userAgent.match(/iPod/i)
                          || window.navigator.userAgent.match(/IEMobile/i)
                          || window.navigator.userAgent.match(/Windows Phone/i)
                          || window.navigator.userAgent.match(/Android/i)
                          || window.navigator.userAgent.match(/BlackBerry/i)
                          || window.navigator.userAgent.match(/webOS/i)) {
                          document.body.className += ' mobile';
                          alert('True - Mobile - ' + navigator.userAgent);
                          } else {
                          alert('False - Mobile - ' + navigator.userAgent);
                          }
                          if(window.navigator.userAgent.match(/Tablet/i)
                          || window.navigator.userAgent.match(/iPad/i)
                          || window.navigator.userAgent.match(/Nexus 7/i)
                          || window.navigator.userAgent.match(/Nexus 10/i)
                          || window.navigator.userAgent.match(/KFAPWI/i)) {
                          document.body.className -= ' mobile';
                          document.body.className += ' tablet';
                          alert('True - Tablet - ' + navigator.userAgent);
                          } else {
                          alert('False - Tablet - ' + navigator.userAgent);
                          }
                          }


                          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:



                          body.tablet { background-color: green; }
                          body.mobile { background-color: red; }


                          alert lines added for development. Chrome console can emulate many handheld devices. Test there.



                          EDIT:



                          Just don't use this, use feature detection instead. There are so many devices and brands out there that targeting a brand NEVER will be the right solution.






                          share|improve this answer















                          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.



                          window.onload = userAgentDetect;
                          function userAgentDetect() {
                          if(window.navigator.userAgent.match(/Mobile/i)
                          || window.navigator.userAgent.match(/iPhone/i)
                          || window.navigator.userAgent.match(/iPod/i)
                          || window.navigator.userAgent.match(/IEMobile/i)
                          || window.navigator.userAgent.match(/Windows Phone/i)
                          || window.navigator.userAgent.match(/Android/i)
                          || window.navigator.userAgent.match(/BlackBerry/i)
                          || window.navigator.userAgent.match(/webOS/i)) {
                          document.body.className += ' mobile';
                          alert('True - Mobile - ' + navigator.userAgent);
                          } else {
                          alert('False - Mobile - ' + navigator.userAgent);
                          }
                          if(window.navigator.userAgent.match(/Tablet/i)
                          || window.navigator.userAgent.match(/iPad/i)
                          || window.navigator.userAgent.match(/Nexus 7/i)
                          || window.navigator.userAgent.match(/Nexus 10/i)
                          || window.navigator.userAgent.match(/KFAPWI/i)) {
                          document.body.className -= ' mobile';
                          document.body.className += ' tablet';
                          alert('True - Tablet - ' + navigator.userAgent);
                          } else {
                          alert('False - Tablet - ' + navigator.userAgent);
                          }
                          }


                          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:



                          body.tablet { background-color: green; }
                          body.mobile { background-color: red; }


                          alert lines added for development. Chrome console can emulate many handheld devices. Test there.



                          EDIT:



                          Just don't use this, use feature detection instead. There are so many devices and brands out there that targeting a brand NEVER will be the right solution.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 16 '17 at 6:35

























                          answered Aug 6 '15 at 19:21









                          LantiLanti

                          96211230




                          96211230























                              4














                              I advise you check out http://wurfl.io/



                              In a nutshell, if you import a tiny JS file:



                              <script type='text/javascript' src="//wurfl.io/wurfl.js"></script>


                              you will be left with a JSON object that looks like:



                              {
                              "complete_device_name":"Google Nexus 7",
                              "is_mobile":true,
                              "form_factor":"Tablet"
                              }


                              (that's assuming you are using a Nexus 7, of course) and you will be able to do things like:



                              if(WURFL.form_factor == "Tablet"){
                              //dostuff();
                              }


                              This is what you are looking for.



                              Disclaimer: I work for the company that offers this free service. Thanks.






                              share|improve this answer



















                              • 1





                                And howcome this does not recognize safari on iphone ?

                                – Amyth
                                Sep 6 '16 at 2:05











                              • Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

                                – Luca Passani
                                Sep 7 '16 at 13:11











                              • I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

                                – Mike Wells
                                Nov 17 '17 at 16:51











                              • I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

                                – Luca Passani
                                Nov 20 '17 at 2:01
















                              4














                              I advise you check out http://wurfl.io/



                              In a nutshell, if you import a tiny JS file:



                              <script type='text/javascript' src="//wurfl.io/wurfl.js"></script>


                              you will be left with a JSON object that looks like:



                              {
                              "complete_device_name":"Google Nexus 7",
                              "is_mobile":true,
                              "form_factor":"Tablet"
                              }


                              (that's assuming you are using a Nexus 7, of course) and you will be able to do things like:



                              if(WURFL.form_factor == "Tablet"){
                              //dostuff();
                              }


                              This is what you are looking for.



                              Disclaimer: I work for the company that offers this free service. Thanks.






                              share|improve this answer



















                              • 1





                                And howcome this does not recognize safari on iphone ?

                                – Amyth
                                Sep 6 '16 at 2:05











                              • Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

                                – Luca Passani
                                Sep 7 '16 at 13:11











                              • I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

                                – Mike Wells
                                Nov 17 '17 at 16:51











                              • I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

                                – Luca Passani
                                Nov 20 '17 at 2:01














                              4












                              4








                              4







                              I advise you check out http://wurfl.io/



                              In a nutshell, if you import a tiny JS file:



                              <script type='text/javascript' src="//wurfl.io/wurfl.js"></script>


                              you will be left with a JSON object that looks like:



                              {
                              "complete_device_name":"Google Nexus 7",
                              "is_mobile":true,
                              "form_factor":"Tablet"
                              }


                              (that's assuming you are using a Nexus 7, of course) and you will be able to do things like:



                              if(WURFL.form_factor == "Tablet"){
                              //dostuff();
                              }


                              This is what you are looking for.



                              Disclaimer: I work for the company that offers this free service. Thanks.






                              share|improve this answer













                              I advise you check out http://wurfl.io/



                              In a nutshell, if you import a tiny JS file:



                              <script type='text/javascript' src="//wurfl.io/wurfl.js"></script>


                              you will be left with a JSON object that looks like:



                              {
                              "complete_device_name":"Google Nexus 7",
                              "is_mobile":true,
                              "form_factor":"Tablet"
                              }


                              (that's assuming you are using a Nexus 7, of course) and you will be able to do things like:



                              if(WURFL.form_factor == "Tablet"){
                              //dostuff();
                              }


                              This is what you are looking for.



                              Disclaimer: I work for the company that offers this free service. Thanks.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered May 5 '16 at 2:34









                              Luca PassaniLuca Passani

                              830715




                              830715








                              • 1





                                And howcome this does not recognize safari on iphone ?

                                – Amyth
                                Sep 6 '16 at 2:05











                              • Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

                                – Luca Passani
                                Sep 7 '16 at 13:11











                              • I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

                                – Mike Wells
                                Nov 17 '17 at 16:51











                              • I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

                                – Luca Passani
                                Nov 20 '17 at 2:01














                              • 1





                                And howcome this does not recognize safari on iphone ?

                                – Amyth
                                Sep 6 '16 at 2:05











                              • Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

                                – Luca Passani
                                Sep 7 '16 at 13:11











                              • I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

                                – Mike Wells
                                Nov 17 '17 at 16:51











                              • I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

                                – Luca Passani
                                Nov 20 '17 at 2:01








                              1




                              1





                              And howcome this does not recognize safari on iphone ?

                              – Amyth
                              Sep 6 '16 at 2:05





                              And howcome this does not recognize safari on iphone ?

                              – Amyth
                              Sep 6 '16 at 2:05













                              Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

                              – Luca Passani
                              Sep 7 '16 at 13:11





                              Can you expand on what browser you are using (exact UA string would be perfect), what data you are getting and what you are expecting?

                              – Luca Passani
                              Sep 7 '16 at 13:11













                              I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

                              – Mike Wells
                              Nov 17 '17 at 16:51





                              I too tried wurfl, I am on a iPhone 5C running IOS 11.2. Its not recognising Safari as a mobile browser. I'm expecting to use "is_mobile" : true and then "form_factor": Smartphone and its not returning either.

                              – Mike Wells
                              Nov 17 '17 at 16:51













                              I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

                              – Luca Passani
                              Nov 20 '17 at 2:01





                              I had to turn to the Mobile Data gurus in the company and they tell me that OS 11.2 doesn't run on the 5C. Lowest device is the 5S. So something isn't right in what you wrote. Feel free to contact ScientiaMobile offline to verify where the disconnect might be. Thanks

                              – Luca Passani
                              Nov 20 '17 at 2:01











                              4














                              Once the element gains focus, you immediately blur it. Bootstrap-datepicker, which is a very popular and well-maintained component with almost 10,000 stars in GitHub, uses this approach:



                              if (window.navigator.maxTouchPoints || 'ontouchstart' in document) {
                              this.input.blur();
                              }


                              https://github.com/uxsolutions/bootstrap-datepicker



                              Thanks to Tigger for assistance.






                              share|improve this answer






























                                4














                                Once the element gains focus, you immediately blur it. Bootstrap-datepicker, which is a very popular and well-maintained component with almost 10,000 stars in GitHub, uses this approach:



                                if (window.navigator.maxTouchPoints || 'ontouchstart' in document) {
                                this.input.blur();
                                }


                                https://github.com/uxsolutions/bootstrap-datepicker



                                Thanks to Tigger for assistance.






                                share|improve this answer




























                                  4












                                  4








                                  4







                                  Once the element gains focus, you immediately blur it. Bootstrap-datepicker, which is a very popular and well-maintained component with almost 10,000 stars in GitHub, uses this approach:



                                  if (window.navigator.maxTouchPoints || 'ontouchstart' in document) {
                                  this.input.blur();
                                  }


                                  https://github.com/uxsolutions/bootstrap-datepicker



                                  Thanks to Tigger for assistance.






                                  share|improve this answer















                                  Once the element gains focus, you immediately blur it. Bootstrap-datepicker, which is a very popular and well-maintained component with almost 10,000 stars in GitHub, uses this approach:



                                  if (window.navigator.maxTouchPoints || 'ontouchstart' in document) {
                                  this.input.blur();
                                  }


                                  https://github.com/uxsolutions/bootstrap-datepicker



                                  Thanks to Tigger for assistance.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Aug 21 '17 at 6:42









                                  Cody Gray

                                  192k35375463




                                  192k35375463










                                  answered Jul 15 '17 at 0:37









                                  J.T. TaylorJ.T. Taylor

                                  2,6241520




                                  2,6241520























                                      4














                                      As many have stated, relying on the moving target of the user agent data is problematic. The same can be said for counting on screen size. My approach is borrowed from a CSS technique to determine if the interface is touch. Using pure javascript (support by all modern browsers), a media query you can easily determine when the device is mobile.



                                      function isMobile() {

                                      if(typeof window.matchMedia != 'undefined' || typeof window.msMatchMedia != 'undefined') {
                                      var mq = window.matchMedia("(pointer:coarse)");
                                      return mq.matches;
                                      }
                                      return false;
                                      }





                                      share|improve this answer
























                                      • This solution works for me, and it up to date...

                                        – RRTW
                                        Oct 1 '18 at 7:18






                                      • 1





                                        What about laptops with touch enabled displays?

                                        – Maxim
                                        Oct 31 '18 at 21:32
















                                      4














                                      As many have stated, relying on the moving target of the user agent data is problematic. The same can be said for counting on screen size. My approach is borrowed from a CSS technique to determine if the interface is touch. Using pure javascript (support by all modern browsers), a media query you can easily determine when the device is mobile.



                                      function isMobile() {

                                      if(typeof window.matchMedia != 'undefined' || typeof window.msMatchMedia != 'undefined') {
                                      var mq = window.matchMedia("(pointer:coarse)");
                                      return mq.matches;
                                      }
                                      return false;
                                      }





                                      share|improve this answer
























                                      • This solution works for me, and it up to date...

                                        – RRTW
                                        Oct 1 '18 at 7:18






                                      • 1





                                        What about laptops with touch enabled displays?

                                        – Maxim
                                        Oct 31 '18 at 21:32














                                      4












                                      4








                                      4







                                      As many have stated, relying on the moving target of the user agent data is problematic. The same can be said for counting on screen size. My approach is borrowed from a CSS technique to determine if the interface is touch. Using pure javascript (support by all modern browsers), a media query you can easily determine when the device is mobile.



                                      function isMobile() {

                                      if(typeof window.matchMedia != 'undefined' || typeof window.msMatchMedia != 'undefined') {
                                      var mq = window.matchMedia("(pointer:coarse)");
                                      return mq.matches;
                                      }
                                      return false;
                                      }





                                      share|improve this answer













                                      As many have stated, relying on the moving target of the user agent data is problematic. The same can be said for counting on screen size. My approach is borrowed from a CSS technique to determine if the interface is touch. Using pure javascript (support by all modern browsers), a media query you can easily determine when the device is mobile.



                                      function isMobile() {

                                      if(typeof window.matchMedia != 'undefined' || typeof window.msMatchMedia != 'undefined') {
                                      var mq = window.matchMedia("(pointer:coarse)");
                                      return mq.matches;
                                      }
                                      return false;
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 9 '18 at 18:57









                                      gsxrboy73gsxrboy73

                                      448311




                                      448311













                                      • This solution works for me, and it up to date...

                                        – RRTW
                                        Oct 1 '18 at 7:18






                                      • 1





                                        What about laptops with touch enabled displays?

                                        – Maxim
                                        Oct 31 '18 at 21:32



















                                      • This solution works for me, and it up to date...

                                        – RRTW
                                        Oct 1 '18 at 7:18






                                      • 1





                                        What about laptops with touch enabled displays?

                                        – Maxim
                                        Oct 31 '18 at 21:32

















                                      This solution works for me, and it up to date...

                                      – RRTW
                                      Oct 1 '18 at 7:18





                                      This solution works for me, and it up to date...

                                      – RRTW
                                      Oct 1 '18 at 7:18




                                      1




                                      1





                                      What about laptops with touch enabled displays?

                                      – Maxim
                                      Oct 31 '18 at 21:32





                                      What about laptops with touch enabled displays?

                                      – Maxim
                                      Oct 31 '18 at 21:32











                                      3














                                      heres a one liner



                                      function isMobile() {
                                      return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
                                      };





                                      share|improve this answer




























                                        3














                                        heres a one liner



                                        function isMobile() {
                                        return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
                                        };





                                        share|improve this answer


























                                          3












                                          3








                                          3







                                          heres a one liner



                                          function isMobile() {
                                          return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
                                          };





                                          share|improve this answer













                                          heres a one liner



                                          function isMobile() {
                                          return (typeof window.orientation !== "undefined") || (navigator.userAgent.indexOf('IEMobile') !== -1);
                                          };






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered May 1 '18 at 21:40









                                          Rick EncisoRick Enciso

                                          8241510




                                          8241510























                                              2














                                              There is simple trick to detect whether it is a mobile device or not. Just check if the ontouchstart event exist:



                                              function isMobile()
                                              {
                                              return "ontouchstart" in window;
                                              }





                                              share|improve this answer
























                                              • Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

                                                – blissfool
                                                Jun 4 '18 at 21:01
















                                              2














                                              There is simple trick to detect whether it is a mobile device or not. Just check if the ontouchstart event exist:



                                              function isMobile()
                                              {
                                              return "ontouchstart" in window;
                                              }





                                              share|improve this answer
























                                              • Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

                                                – blissfool
                                                Jun 4 '18 at 21:01














                                              2












                                              2








                                              2







                                              There is simple trick to detect whether it is a mobile device or not. Just check if the ontouchstart event exist:



                                              function isMobile()
                                              {
                                              return "ontouchstart" in window;
                                              }





                                              share|improve this answer













                                              There is simple trick to detect whether it is a mobile device or not. Just check if the ontouchstart event exist:



                                              function isMobile()
                                              {
                                              return "ontouchstart" in window;
                                              }






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jan 25 '18 at 10:20









                                              Martin WantkeMartin Wantke

                                              1,6971610




                                              1,6971610













                                              • Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

                                                – blissfool
                                                Jun 4 '18 at 21:01



















                                              • Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

                                                – blissfool
                                                Jun 4 '18 at 21:01

















                                              Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

                                              – blissfool
                                              Jun 4 '18 at 21:01





                                              Won't work with laptops and dekstops with touchscreen monitors. Also will be an issue with hybrid pc's like Surface. Less of an issue with desktops but there are more touchscreen laptops being sold these days.

                                              – blissfool
                                              Jun 4 '18 at 21:01











                                              2














                                              According to MDN's article on Browser detection using the user agent, it is encouraged to avoid this approach if possible and suggest other avenues such as feature detection.



                                              However, if one must use the user agent as a means to detect if the device is mobile, they suggest:




                                              In summary, we recommend looking for the string “Mobi” anywhere in the
                                              User Agent to detect a mobile device.




                                              Therefore, this one-liner will suffice:



                                              const isMobileDevice = window.navigator.userAgent.toLowerCase().includes("mobi");






                                              share|improve this answer


























                                              • .includes not sopport by IE-11

                                                – Pasha Oleynik
                                                Sep 3 '18 at 12:57











                                              • @PashaOleynik a polyfill can fix that

                                                – Maxim
                                                Oct 31 '18 at 21:35
















                                              2














                                              According to MDN's article on Browser detection using the user agent, it is encouraged to avoid this approach if possible and suggest other avenues such as feature detection.



                                              However, if one must use the user agent as a means to detect if the device is mobile, they suggest:




                                              In summary, we recommend looking for the string “Mobi” anywhere in the
                                              User Agent to detect a mobile device.




                                              Therefore, this one-liner will suffice:



                                              const isMobileDevice = window.navigator.userAgent.toLowerCase().includes("mobi");






                                              share|improve this answer


























                                              • .includes not sopport by IE-11

                                                – Pasha Oleynik
                                                Sep 3 '18 at 12:57











                                              • @PashaOleynik a polyfill can fix that

                                                – Maxim
                                                Oct 31 '18 at 21:35














                                              2












                                              2








                                              2







                                              According to MDN's article on Browser detection using the user agent, it is encouraged to avoid this approach if possible and suggest other avenues such as feature detection.



                                              However, if one must use the user agent as a means to detect if the device is mobile, they suggest:




                                              In summary, we recommend looking for the string “Mobi” anywhere in the
                                              User Agent to detect a mobile device.




                                              Therefore, this one-liner will suffice:



                                              const isMobileDevice = window.navigator.userAgent.toLowerCase().includes("mobi");






                                              share|improve this answer















                                              According to MDN's article on Browser detection using the user agent, it is encouraged to avoid this approach if possible and suggest other avenues such as feature detection.



                                              However, if one must use the user agent as a means to detect if the device is mobile, they suggest:




                                              In summary, we recommend looking for the string “Mobi” anywhere in the
                                              User Agent to detect a mobile device.




                                              Therefore, this one-liner will suffice:



                                              const isMobileDevice = window.navigator.userAgent.toLowerCase().includes("mobi");







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Feb 3 '18 at 18:58









                                              Boy With Silver Wings

                                              4,4012732




                                              4,4012732










                                              answered Aug 14 '17 at 1:53









                                              TheDarkIn1978TheDarkIn1978

                                              9,7201261131




                                              9,7201261131













                                              • .includes not sopport by IE-11

                                                – Pasha Oleynik
                                                Sep 3 '18 at 12:57











                                              • @PashaOleynik a polyfill can fix that

                                                – Maxim
                                                Oct 31 '18 at 21:35



















                                              • .includes not sopport by IE-11

                                                – Pasha Oleynik
                                                Sep 3 '18 at 12:57











                                              • @PashaOleynik a polyfill can fix that

                                                – Maxim
                                                Oct 31 '18 at 21:35

















                                              .includes not sopport by IE-11

                                              – Pasha Oleynik
                                              Sep 3 '18 at 12:57





                                              .includes not sopport by IE-11

                                              – Pasha Oleynik
                                              Sep 3 '18 at 12:57













                                              @PashaOleynik a polyfill can fix that

                                              – Maxim
                                              Oct 31 '18 at 21:35





                                              @PashaOleynik a polyfill can fix that

                                              – Maxim
                                              Oct 31 '18 at 21:35











                                              1














                                              The best must be :



                                              var isMobile = (/Mobile/i.test(navigator.userAgent));


                                              But like like Yoav Barnea says...



                                              // Seem legit
                                              var isMobile = ('DeviceOrientationEvent' in window || 'orientation' in window);
                                              // But with my Chrome on windows, DeviceOrientationEvent == fct()
                                              if (/Windows NT|Macintosh|Mac OS X|Linux/i.test(navigator.userAgent)) isMobile = false;
                                              // My android have "linux" too
                                              if (/Mobile/i.test(navigator.userAgent)) isMobile = true;


                                              After this 3 tests, i hope var isMobile is... ok






                                              share|improve this answer


























                                              • > Firefox mobile on android doesn't seem to have "'orientation' in window"

                                                – molokoloco
                                                May 30 '13 at 12:38






                                              • 1





                                                Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

                                                – molokoloco
                                                Jun 1 '13 at 13:31













                                              • Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

                                                – B2K
                                                Jul 10 '15 at 14:27











                                              • To make it more elegant you should make all te code in just one if-else if-else if block.

                                                – Heitor
                                                Aug 4 '18 at 6:56
















                                              1














                                              The best must be :



                                              var isMobile = (/Mobile/i.test(navigator.userAgent));


                                              But like like Yoav Barnea says...



                                              // Seem legit
                                              var isMobile = ('DeviceOrientationEvent' in window || 'orientation' in window);
                                              // But with my Chrome on windows, DeviceOrientationEvent == fct()
                                              if (/Windows NT|Macintosh|Mac OS X|Linux/i.test(navigator.userAgent)) isMobile = false;
                                              // My android have "linux" too
                                              if (/Mobile/i.test(navigator.userAgent)) isMobile = true;


                                              After this 3 tests, i hope var isMobile is... ok






                                              share|improve this answer


























                                              • > Firefox mobile on android doesn't seem to have "'orientation' in window"

                                                – molokoloco
                                                May 30 '13 at 12:38






                                              • 1





                                                Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

                                                – molokoloco
                                                Jun 1 '13 at 13:31













                                              • Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

                                                – B2K
                                                Jul 10 '15 at 14:27











                                              • To make it more elegant you should make all te code in just one if-else if-else if block.

                                                – Heitor
                                                Aug 4 '18 at 6:56














                                              1












                                              1








                                              1







                                              The best must be :



                                              var isMobile = (/Mobile/i.test(navigator.userAgent));


                                              But like like Yoav Barnea says...



                                              // Seem legit
                                              var isMobile = ('DeviceOrientationEvent' in window || 'orientation' in window);
                                              // But with my Chrome on windows, DeviceOrientationEvent == fct()
                                              if (/Windows NT|Macintosh|Mac OS X|Linux/i.test(navigator.userAgent)) isMobile = false;
                                              // My android have "linux" too
                                              if (/Mobile/i.test(navigator.userAgent)) isMobile = true;


                                              After this 3 tests, i hope var isMobile is... ok






                                              share|improve this answer















                                              The best must be :



                                              var isMobile = (/Mobile/i.test(navigator.userAgent));


                                              But like like Yoav Barnea says...



                                              // Seem legit
                                              var isMobile = ('DeviceOrientationEvent' in window || 'orientation' in window);
                                              // But with my Chrome on windows, DeviceOrientationEvent == fct()
                                              if (/Windows NT|Macintosh|Mac OS X|Linux/i.test(navigator.userAgent)) isMobile = false;
                                              // My android have "linux" too
                                              if (/Mobile/i.test(navigator.userAgent)) isMobile = true;


                                              After this 3 tests, i hope var isMobile is... ok







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 30 '13 at 22:20

























                                              answered May 30 '13 at 12:34









                                              molokolocomolokoloco

                                              3,62712523




                                              3,62712523













                                              • > Firefox mobile on android doesn't seem to have "'orientation' in window"

                                                – molokoloco
                                                May 30 '13 at 12:38






                                              • 1





                                                Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

                                                – molokoloco
                                                Jun 1 '13 at 13:31













                                              • Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

                                                – B2K
                                                Jul 10 '15 at 14:27











                                              • To make it more elegant you should make all te code in just one if-else if-else if block.

                                                – Heitor
                                                Aug 4 '18 at 6:56



















                                              • > Firefox mobile on android doesn't seem to have "'orientation' in window"

                                                – molokoloco
                                                May 30 '13 at 12:38






                                              • 1





                                                Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

                                                – molokoloco
                                                Jun 1 '13 at 13:31













                                              • Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

                                                – B2K
                                                Jul 10 '15 at 14:27











                                              • To make it more elegant you should make all te code in just one if-else if-else if block.

                                                – Heitor
                                                Aug 4 '18 at 6:56

















                                              > Firefox mobile on android doesn't seem to have "'orientation' in window"

                                              – molokoloco
                                              May 30 '13 at 12:38





                                              > Firefox mobile on android doesn't seem to have "'orientation' in window"

                                              – molokoloco
                                              May 30 '13 at 12:38




                                              1




                                              1





                                              Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

                                              – molokoloco
                                              Jun 1 '13 at 13:31







                                              Sorry.. ok for me it work well like that now. "if (Modernizr.touch) /* ... */ " and go on...

                                              – molokoloco
                                              Jun 1 '13 at 13:31















                                              Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

                                              – B2K
                                              Jul 10 '15 at 14:27





                                              Just wondering how Modernizr.touch would work when on a touchscreen desktop device.

                                              – B2K
                                              Jul 10 '15 at 14:27













                                              To make it more elegant you should make all te code in just one if-else if-else if block.

                                              – Heitor
                                              Aug 4 '18 at 6:56





                                              To make it more elegant you should make all te code in just one if-else if-else if block.

                                              – Heitor
                                              Aug 4 '18 at 6:56











                                              1














                                              Here is he full function



                                              function isMobile(){
                                              return (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i.test(navigator.userAgent||navigator.vendor||window.opera)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test((navigator.userAgent||navigator.vendor||window.opera).substr(0,4)))
                                              }

                                              jQuery.noConflict();
                                              jQuery(document).ready(function(){
                                              if(isMobile()) alert("Mobile"); else alert("Not Mobile");
                                              });





                                              share|improve this answer


























                                              • .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

                                                – raacer
                                                Aug 24 '15 at 21:31






                                              • 1





                                                @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

                                                – JackW
                                                Aug 27 '15 at 13:45
















                                              1














                                              Here is he full function



                                              function isMobile(){
                                              return (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i.test(navigator.userAgent||navigator.vendor||window.opera)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test((navigator.userAgent||navigator.vendor||window.opera).substr(0,4)))
                                              }

                                              jQuery.noConflict();
                                              jQuery(document).ready(function(){
                                              if(isMobile()) alert("Mobile"); else alert("Not Mobile");
                                              });





                                              share|improve this answer


























                                              • .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

                                                – raacer
                                                Aug 24 '15 at 21:31






                                              • 1





                                                @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

                                                – JackW
                                                Aug 27 '15 at 13:45














                                              1












                                              1








                                              1







                                              Here is he full function



                                              function isMobile(){
                                              return (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i.test(navigator.userAgent||navigator.vendor||window.opera)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test((navigator.userAgent||navigator.vendor||window.opera).substr(0,4)))
                                              }

                                              jQuery.noConflict();
                                              jQuery(document).ready(function(){
                                              if(isMobile()) alert("Mobile"); else alert("Not Mobile");
                                              });





                                              share|improve this answer















                                              Here is he full function



                                              function isMobile(){
                                              return (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino|android|ipad|playbook|silk/i.test(navigator.userAgent||navigator.vendor||window.opera)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test((navigator.userAgent||navigator.vendor||window.opera).substr(0,4)))
                                              }

                                              jQuery.noConflict();
                                              jQuery(document).ready(function(){
                                              if(isMobile()) alert("Mobile"); else alert("Not Mobile");
                                              });






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Jun 1 '13 at 1:03









                                              Fred Wuerges

                                              1,66421842




                                              1,66421842










                                              answered Jun 1 '13 at 0:25









                                              David LattyDavid Latty

                                              364




                                              364













                                              • .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

                                                – raacer
                                                Aug 24 '15 at 21:31






                                              • 1





                                                @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

                                                – JackW
                                                Aug 27 '15 at 13:45



















                                              • .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

                                                – raacer
                                                Aug 24 '15 at 21:31






                                              • 1





                                                @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

                                                – JackW
                                                Aug 27 '15 at 13:45

















                                              .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

                                              – raacer
                                              Aug 24 '15 at 21:31





                                              .substr(0,4) returns first 4 letters. How does it detect "android.+mobile"?

                                              – raacer
                                              Aug 24 '15 at 21:31




                                              1




                                              1





                                              @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

                                              – JackW
                                              Aug 27 '15 at 13:45





                                              @raacer there are actually two regexes in the answer (both on the same line) - the first one checks against the entire UA string, and looks for android, mobile etc, while the second one only checks against the first 4 characters of the UA.

                                              – JackW
                                              Aug 27 '15 at 13:45











                                              1














                                              //true / false
                                              function isMobile()
                                              {
                                              return (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) );
                                              }


                                              also you can follow this tutorial to detect a specific mobile. Click here.






                                              share|improve this answer
























                                              • Please add Mobile to your RX

                                                – oriadam
                                                Jul 11 '16 at 11:05
















                                              1














                                              //true / false
                                              function isMobile()
                                              {
                                              return (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) );
                                              }


                                              also you can follow this tutorial to detect a specific mobile. Click here.






                                              share|improve this answer
























                                              • Please add Mobile to your RX

                                                – oriadam
                                                Jul 11 '16 at 11:05














                                              1












                                              1








                                              1







                                              //true / false
                                              function isMobile()
                                              {
                                              return (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) );
                                              }


                                              also you can follow this tutorial to detect a specific mobile. Click here.






                                              share|improve this answer













                                              //true / false
                                              function isMobile()
                                              {
                                              return (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) );
                                              }


                                              also you can follow this tutorial to detect a specific mobile. Click here.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Apr 29 '16 at 8:33









                                              gtzinosgtzinos

                                              932924




                                              932924













                                              • Please add Mobile to your RX

                                                – oriadam
                                                Jul 11 '16 at 11:05



















                                              • Please add Mobile to your RX

                                                – oriadam
                                                Jul 11 '16 at 11:05

















                                              Please add Mobile to your RX

                                              – oriadam
                                              Jul 11 '16 at 11:05





                                              Please add Mobile to your RX

                                              – oriadam
                                              Jul 11 '16 at 11:05











                                              1














                                              what about using "window.screen.width" ?



                                              if (window.screen.width < 800) {
                                              // do something
                                              }


                                              or



                                              if($(window).width() < 800) {
                                              //do something
                                              }


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






                                              share|improve this answer





















                                              • 1





                                                What is about landscape?

                                                – Erick Voodoo
                                                Apr 10 '17 at 11:29






                                              • 1





                                                This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

                                                – toing_toing
                                                May 7 '17 at 16:43











                                              • A PC is essentially different from mobile devices on usability, horrable answer!!

                                                – Heitor
                                                Aug 4 '18 at 7:01
















                                              1














                                              what about using "window.screen.width" ?



                                              if (window.screen.width < 800) {
                                              // do something
                                              }


                                              or



                                              if($(window).width() < 800) {
                                              //do something
                                              }


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






                                              share|improve this answer





















                                              • 1





                                                What is about landscape?

                                                – Erick Voodoo
                                                Apr 10 '17 at 11:29






                                              • 1





                                                This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

                                                – toing_toing
                                                May 7 '17 at 16:43











                                              • A PC is essentially different from mobile devices on usability, horrable answer!!

                                                – Heitor
                                                Aug 4 '18 at 7:01














                                              1












                                              1








                                              1







                                              what about using "window.screen.width" ?



                                              if (window.screen.width < 800) {
                                              // do something
                                              }


                                              or



                                              if($(window).width() < 800) {
                                              //do something
                                              }


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






                                              share|improve this answer















                                              what about using "window.screen.width" ?



                                              if (window.screen.width < 800) {
                                              // do something
                                              }


                                              or



                                              if($(window).width() < 800) {
                                              //do something
                                              }


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







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 25 '16 at 10:27

























                                              answered May 25 '16 at 7:18









                                              Ahmad YousefAhmad Yousef

                                              315311




                                              315311








                                              • 1





                                                What is about landscape?

                                                – Erick Voodoo
                                                Apr 10 '17 at 11:29






                                              • 1





                                                This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

                                                – toing_toing
                                                May 7 '17 at 16:43











                                              • A PC is essentially different from mobile devices on usability, horrable answer!!

                                                – Heitor
                                                Aug 4 '18 at 7:01














                                              • 1





                                                What is about landscape?

                                                – Erick Voodoo
                                                Apr 10 '17 at 11:29






                                              • 1





                                                This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

                                                – toing_toing
                                                May 7 '17 at 16:43











                                              • A PC is essentially different from mobile devices on usability, horrable answer!!

                                                – Heitor
                                                Aug 4 '18 at 7:01








                                              1




                                              1





                                              What is about landscape?

                                              – Erick Voodoo
                                              Apr 10 '17 at 11:29





                                              What is about landscape?

                                              – Erick Voodoo
                                              Apr 10 '17 at 11:29




                                              1




                                              1





                                              This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

                                              – toing_toing
                                              May 7 '17 at 16:43





                                              This is not very useful for certain scenarios. If the desktop browser is resized it maybe detected incorrectly as a mobile browser

                                              – toing_toing
                                              May 7 '17 at 16:43













                                              A PC is essentially different from mobile devices on usability, horrable answer!!

                                              – Heitor
                                              Aug 4 '18 at 7:01





                                              A PC is essentially different from mobile devices on usability, horrable answer!!

                                              – Heitor
                                              Aug 4 '18 at 7:01











                                              1














                                              Note that Most newer-gen mobile devices now have resolutions greater than 600x400. ie, an iPhone 6....



                                              Proof of test:
                                              ran the most upvoted and most recent posts here, with an optional check once run like so:



                                              (function(a){
                                              window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))
                                              })(navigator.userAgent||navigator.vendor||window.opera);

                                              alert("This browser was found to be a % browser.", window.isMobile ? 'mobile' : 'desktop');


                                              Somehow, the following results were returned on the following Browser Apps. Specs: iPhone 6S, iOS 10.3.1.



                                              Safari (latest): Detected it as a mobile.



                                              Chrome (latest): Did not detect it as a mobile.



                                              SO, i then tested the suggestion from Lanti (https://stackoverflow.com/a/31864119/7183483), and it did return the proper results (mobile for all iOS devices, and desktop for my Mac). Therefore, i proceeded to edit it a little since it would fire twice (for both mobile and Tablet). I then noticed when testing on an iPad, that it also returned as a mobile, which makes sense, since the Parameters that Lanti uses check the OS more than anything. Therefore, i simply moved the tablet IF statement inside the mobile check, which would return mobile is the Tablet check was negative, and tablet otherwise. I then added the else clause for the mobile check to return as desktop/laptop, since both qualify, but then noticed that the browser detects CPU and OS brand. So i added what is returned in there as part of else if statement instead. To cap it, I added a cautionary else statement in case nothing was detected.
                                              See bellow, will update with a test on a Windows 10 PC soon.



                                              Oh, and i also added a 'debugMode' variable, to easily switch between debug and normal compiling.



                                              Dislaimer: Full credit to Lanti, also that this was not tested on Windows Tablets... which might return desktop/laptop, since the OS is pure Windows. Will check once I find a friend who uses one.



                                              function userAgentDetect() {
                                              let debugMode = true;
                                              if(window.navigator.userAgent.match(/Mobile/i)
                                              || window.navigator.userAgent.match(/iPhone/i)
                                              || window.navigator.userAgent.match(/iPod/i)
                                              || window.navigator.userAgent.match(/IEMobile/i)
                                              || window.navigator.userAgent.match(/Windows Phone/i)
                                              || window.navigator.userAgent.match(/Android/i)
                                              || window.navigator.userAgent.match(/BlackBerry/i)
                                              || window.navigator.userAgent.match(/webOS/i)) {
                                              if (window.navigator.userAgent.match(/Tablet/i)
                                              || window.navigator.userAgent.match(/iPad/i)
                                              || window.navigator.userAgent.match(/Nexus 7/i)
                                              || window.navigator.userAgent.match(/Nexus 10/i)
                                              || window.navigator.userAgent.match(/KFAPWI/i)) {
                                              window.deviceTypeVar = 'tablet';
                                              if (debugMode === true) {
                                              alert('Device is a tablet - ' + navigator.userAgent);
                                              }
                                              } else {
                                              if (debugMode === true) {
                                              alert('Device is a smartphone - ' + navigator.userAgent);
                                              };
                                              window.deviceTypeVar = 'smartphone';
                                              }
                                              } else if (window.navigator.userAgent.match(/Intel Mac/i)) {
                                              if (debugMode === true) {
                                              alert('Device is a desktop or laptop- ' + navigator.userAgent);
                                              }
                                              window.deviceTypeVar = 'desktop_or_laptop';
                                              } else if (window.navigator.userAgent.match(/Nexus 7/i)
                                              || window.navigator.userAgent.match(/Nexus 10/i)
                                              || window.navigator.userAgent.match(/KFAPWI/i)) {
                                              window.deviceTypeVar = 'tablet';
                                              if (debugMode === true) {
                                              alert('Device is a tablet - ' + navigator.userAgent);
                                              }
                                              } else {
                                              if (debugMode === true) {
                                              alert('Device is unknown- ' + navigator.userAgent);
                                              }
                                              window.deviceTypeVar = 'Unknown';
                                              }
                                              }





                                              share|improve this answer






























                                                1














                                                Note that Most newer-gen mobile devices now have resolutions greater than 600x400. ie, an iPhone 6....



                                                Proof of test:
                                                ran the most upvoted and most recent posts here, with an optional check once run like so:



                                                (function(a){
                                                window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))
                                                })(navigator.userAgent||navigator.vendor||window.opera);

                                                alert("This browser was found to be a % browser.", window.isMobile ? 'mobile' : 'desktop');


                                                Somehow, the following results were returned on the following Browser Apps. Specs: iPhone 6S, iOS 10.3.1.



                                                Safari (latest): Detected it as a mobile.



                                                Chrome (latest): Did not detect it as a mobile.



                                                SO, i then tested the suggestion from Lanti (https://stackoverflow.com/a/31864119/7183483), and it did return the proper results (mobile for all iOS devices, and desktop for my Mac). Therefore, i proceeded to edit it a little since it would fire twice (for both mobile and Tablet). I then noticed when testing on an iPad, that it also returned as a mobile, which makes sense, since the Parameters that Lanti uses check the OS more than anything. Therefore, i simply moved the tablet IF statement inside the mobile check, which would return mobile is the Tablet check was negative, and tablet otherwise. I then added the else clause for the mobile check to return as desktop/laptop, since both qualify, but then noticed that the browser detects CPU and OS brand. So i added what is returned in there as part of else if statement instead. To cap it, I added a cautionary else statement in case nothing was detected.
                                                See bellow, will update with a test on a Windows 10 PC soon.



                                                Oh, and i also added a 'debugMode' variable, to easily switch between debug and normal compiling.



                                                Dislaimer: Full credit to Lanti, also that this was not tested on Windows Tablets... which might return desktop/laptop, since the OS is pure Windows. Will check once I find a friend who uses one.



                                                function userAgentDetect() {
                                                let debugMode = true;
                                                if(window.navigator.userAgent.match(/Mobile/i)
                                                || window.navigator.userAgent.match(/iPhone/i)
                                                || window.navigator.userAgent.match(/iPod/i)
                                                || window.navigator.userAgent.match(/IEMobile/i)
                                                || window.navigator.userAgent.match(/Windows Phone/i)
                                                || window.navigator.userAgent.match(/Android/i)
                                                || window.navigator.userAgent.match(/BlackBerry/i)
                                                || window.navigator.userAgent.match(/webOS/i)) {
                                                if (window.navigator.userAgent.match(/Tablet/i)
                                                || window.navigator.userAgent.match(/iPad/i)
                                                || window.navigator.userAgent.match(/Nexus 7/i)
                                                || window.navigator.userAgent.match(/Nexus 10/i)
                                                || window.navigator.userAgent.match(/KFAPWI/i)) {
                                                window.deviceTypeVar = 'tablet';
                                                if (debugMode === true) {
                                                alert('Device is a tablet - ' + navigator.userAgent);
                                                }
                                                } else {
                                                if (debugMode === true) {
                                                alert('Device is a smartphone - ' + navigator.userAgent);
                                                };
                                                window.deviceTypeVar = 'smartphone';
                                                }
                                                } else if (window.navigator.userAgent.match(/Intel Mac/i)) {
                                                if (debugMode === true) {
                                                alert('Device is a desktop or laptop- ' + navigator.userAgent);
                                                }
                                                window.deviceTypeVar = 'desktop_or_laptop';
                                                } else if (window.navigator.userAgent.match(/Nexus 7/i)
                                                || window.navigator.userAgent.match(/Nexus 10/i)
                                                || window.navigator.userAgent.match(/KFAPWI/i)) {
                                                window.deviceTypeVar = 'tablet';
                                                if (debugMode === true) {
                                                alert('Device is a tablet - ' + navigator.userAgent);
                                                }
                                                } else {
                                                if (debugMode === true) {
                                                alert('Device is unknown- ' + navigator.userAgent);
                                                }
                                                window.deviceTypeVar = 'Unknown';
                                                }
                                                }





                                                share|improve this answer




























                                                  1












                                                  1








                                                  1







                                                  Note that Most newer-gen mobile devices now have resolutions greater than 600x400. ie, an iPhone 6....



                                                  Proof of test:
                                                  ran the most upvoted and most recent posts here, with an optional check once run like so:



                                                  (function(a){
                                                  window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))
                                                  })(navigator.userAgent||navigator.vendor||window.opera);

                                                  alert("This browser was found to be a % browser.", window.isMobile ? 'mobile' : 'desktop');


                                                  Somehow, the following results were returned on the following Browser Apps. Specs: iPhone 6S, iOS 10.3.1.



                                                  Safari (latest): Detected it as a mobile.



                                                  Chrome (latest): Did not detect it as a mobile.



                                                  SO, i then tested the suggestion from Lanti (https://stackoverflow.com/a/31864119/7183483), and it did return the proper results (mobile for all iOS devices, and desktop for my Mac). Therefore, i proceeded to edit it a little since it would fire twice (for both mobile and Tablet). I then noticed when testing on an iPad, that it also returned as a mobile, which makes sense, since the Parameters that Lanti uses check the OS more than anything. Therefore, i simply moved the tablet IF statement inside the mobile check, which would return mobile is the Tablet check was negative, and tablet otherwise. I then added the else clause for the mobile check to return as desktop/laptop, since both qualify, but then noticed that the browser detects CPU and OS brand. So i added what is returned in there as part of else if statement instead. To cap it, I added a cautionary else statement in case nothing was detected.
                                                  See bellow, will update with a test on a Windows 10 PC soon.



                                                  Oh, and i also added a 'debugMode' variable, to easily switch between debug and normal compiling.



                                                  Dislaimer: Full credit to Lanti, also that this was not tested on Windows Tablets... which might return desktop/laptop, since the OS is pure Windows. Will check once I find a friend who uses one.



                                                  function userAgentDetect() {
                                                  let debugMode = true;
                                                  if(window.navigator.userAgent.match(/Mobile/i)
                                                  || window.navigator.userAgent.match(/iPhone/i)
                                                  || window.navigator.userAgent.match(/iPod/i)
                                                  || window.navigator.userAgent.match(/IEMobile/i)
                                                  || window.navigator.userAgent.match(/Windows Phone/i)
                                                  || window.navigator.userAgent.match(/Android/i)
                                                  || window.navigator.userAgent.match(/BlackBerry/i)
                                                  || window.navigator.userAgent.match(/webOS/i)) {
                                                  if (window.navigator.userAgent.match(/Tablet/i)
                                                  || window.navigator.userAgent.match(/iPad/i)
                                                  || window.navigator.userAgent.match(/Nexus 7/i)
                                                  || window.navigator.userAgent.match(/Nexus 10/i)
                                                  || window.navigator.userAgent.match(/KFAPWI/i)) {
                                                  window.deviceTypeVar = 'tablet';
                                                  if (debugMode === true) {
                                                  alert('Device is a tablet - ' + navigator.userAgent);
                                                  }
                                                  } else {
                                                  if (debugMode === true) {
                                                  alert('Device is a smartphone - ' + navigator.userAgent);
                                                  };
                                                  window.deviceTypeVar = 'smartphone';
                                                  }
                                                  } else if (window.navigator.userAgent.match(/Intel Mac/i)) {
                                                  if (debugMode === true) {
                                                  alert('Device is a desktop or laptop- ' + navigator.userAgent);
                                                  }
                                                  window.deviceTypeVar = 'desktop_or_laptop';
                                                  } else if (window.navigator.userAgent.match(/Nexus 7/i)
                                                  || window.navigator.userAgent.match(/Nexus 10/i)
                                                  || window.navigator.userAgent.match(/KFAPWI/i)) {
                                                  window.deviceTypeVar = 'tablet';
                                                  if (debugMode === true) {
                                                  alert('Device is a tablet - ' + navigator.userAgent);
                                                  }
                                                  } else {
                                                  if (debugMode === true) {
                                                  alert('Device is unknown- ' + navigator.userAgent);
                                                  }
                                                  window.deviceTypeVar = 'Unknown';
                                                  }
                                                  }





                                                  share|improve this answer















                                                  Note that Most newer-gen mobile devices now have resolutions greater than 600x400. ie, an iPhone 6....



                                                  Proof of test:
                                                  ran the most upvoted and most recent posts here, with an optional check once run like so:



                                                  (function(a){
                                                  window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))
                                                  })(navigator.userAgent||navigator.vendor||window.opera);

                                                  alert("This browser was found to be a % browser.", window.isMobile ? 'mobile' : 'desktop');


                                                  Somehow, the following results were returned on the following Browser Apps. Specs: iPhone 6S, iOS 10.3.1.



                                                  Safari (latest): Detected it as a mobile.



                                                  Chrome (latest): Did not detect it as a mobile.



                                                  SO, i then tested the suggestion from Lanti (https://stackoverflow.com/a/31864119/7183483), and it did return the proper results (mobile for all iOS devices, and desktop for my Mac). Therefore, i proceeded to edit it a little since it would fire twice (for both mobile and Tablet). I then noticed when testing on an iPad, that it also returned as a mobile, which makes sense, since the Parameters that Lanti uses check the OS more than anything. Therefore, i simply moved the tablet IF statement inside the mobile check, which would return mobile is the Tablet check was negative, and tablet otherwise. I then added the else clause for the mobile check to return as desktop/laptop, since both qualify, but then noticed that the browser detects CPU and OS brand. So i added what is returned in there as part of else if statement instead. To cap it, I added a cautionary else statement in case nothing was detected.
                                                  See bellow, will update with a test on a Windows 10 PC soon.



                                                  Oh, and i also added a 'debugMode' variable, to easily switch between debug and normal compiling.



                                                  Dislaimer: Full credit to Lanti, also that this was not tested on Windows Tablets... which might return desktop/laptop, since the OS is pure Windows. Will check once I find a friend who uses one.



                                                  function userAgentDetect() {
                                                  let debugMode = true;
                                                  if(window.navigator.userAgent.match(/Mobile/i)
                                                  || window.navigator.userAgent.match(/iPhone/i)
                                                  || window.navigator.userAgent.match(/iPod/i)
                                                  || window.navigator.userAgent.match(/IEMobile/i)
                                                  || window.navigator.userAgent.match(/Windows Phone/i)
                                                  || window.navigator.userAgent.match(/Android/i)
                                                  || window.navigator.userAgent.match(/BlackBerry/i)
                                                  || window.navigator.userAgent.match(/webOS/i)) {
                                                  if (window.navigator.userAgent.match(/Tablet/i)
                                                  || window.navigator.userAgent.match(/iPad/i)
                                                  || window.navigator.userAgent.match(/Nexus 7/i)
                                                  || window.navigator.userAgent.match(/Nexus 10/i)
                                                  || window.navigator.userAgent.match(/KFAPWI/i)) {
                                                  window.deviceTypeVar = 'tablet';
                                                  if (debugMode === true) {
                                                  alert('Device is a tablet - ' + navigator.userAgent);
                                                  }
                                                  } else {
                                                  if (debugMode === true) {
                                                  alert('Device is a smartphone - ' + navigator.userAgent);
                                                  };
                                                  window.deviceTypeVar = 'smartphone';
                                                  }
                                                  } else if (window.navigator.userAgent.match(/Intel Mac/i)) {
                                                  if (debugMode === true) {
                                                  alert('Device is a desktop or laptop- ' + navigator.userAgent);
                                                  }
                                                  window.deviceTypeVar = 'desktop_or_laptop';
                                                  } else if (window.navigator.userAgent.match(/Nexus 7/i)
                                                  || window.navigator.userAgent.match(/Nexus 10/i)
                                                  || window.navigator.userAgent.match(/KFAPWI/i)) {
                                                  window.deviceTypeVar = 'tablet';
                                                  if (debugMode === true) {
                                                  alert('Device is a tablet - ' + navigator.userAgent);
                                                  }
                                                  } else {
                                                  if (debugMode === true) {
                                                  alert('Device is unknown- ' + navigator.userAgent);
                                                  }
                                                  window.deviceTypeVar = 'Unknown';
                                                  }
                                                  }






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited May 23 '17 at 12:26









                                                  Community

                                                  11




                                                  11










                                                  answered May 11 '17 at 19:56









                                                  murphguymurphguy

                                                  887516




                                                  887516























                                                      1














                                                      This is just an es6 port of the accepted answer that I'm using in my project. Note that this also includes tablets.



                                                      export const isMobile = () => {
                                                      const vendor = navigator.userAgent || navigator.vendor || window.opera;

                                                      return !!(
                                                      /(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(
                                                      vendor
                                                      ) ||
                                                      /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(
                                                      vendor.substr(0, 4)
                                                      )
                                                      );
                                                      };





                                                      share|improve this answer




























                                                        1














                                                        This is just an es6 port of the accepted answer that I'm using in my project. Note that this also includes tablets.



                                                        export const isMobile = () => {
                                                        const vendor = navigator.userAgent || navigator.vendor || window.opera;

                                                        return !!(
                                                        /(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(
                                                        vendor
                                                        ) ||
                                                        /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(
                                                        vendor.substr(0, 4)
                                                        )
                                                        );
                                                        };





                                                        share|improve this answer


























                                                          1












                                                          1








                                                          1







                                                          This is just an es6 port of the accepted answer that I'm using in my project. Note that this also includes tablets.



                                                          export const isMobile = () => {
                                                          const vendor = navigator.userAgent || navigator.vendor || window.opera;

                                                          return !!(
                                                          /(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(
                                                          vendor
                                                          ) ||
                                                          /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(
                                                          vendor.substr(0, 4)
                                                          )
                                                          );
                                                          };





                                                          share|improve this answer













                                                          This is just an es6 port of the accepted answer that I'm using in my project. Note that this also includes tablets.



                                                          export const isMobile = () => {
                                                          const vendor = navigator.userAgent || navigator.vendor || window.opera;

                                                          return !!(
                                                          /(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(
                                                          vendor
                                                          ) ||
                                                          /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(
                                                          vendor.substr(0, 4)
                                                          )
                                                          );
                                                          };






                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jan 4 '18 at 16:27









                                                          AndiAndi

                                                          623710




                                                          623710























                                                              1

















                                                              return 'ontouchstart' in window && window.screen.availWidth < 768





                                                              How about this, it expands on answer above but also checks the screen size






                                                              share|improve this answer




























                                                                1

















                                                                return 'ontouchstart' in window && window.screen.availWidth < 768





                                                                How about this, it expands on answer above but also checks the screen size






                                                                share|improve this answer


























                                                                  1












                                                                  1








                                                                  1










                                                                  return 'ontouchstart' in window && window.screen.availWidth < 768





                                                                  How about this, it expands on answer above but also checks the screen size






                                                                  share|improve this answer
















                                                                  return 'ontouchstart' in window && window.screen.availWidth < 768





                                                                  How about this, it expands on answer above but also checks the screen size






                                                                  return 'ontouchstart' in window && window.screen.availWidth < 768





                                                                  return 'ontouchstart' in window && window.screen.availWidth < 768






                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Apr 30 '18 at 23:20









                                                                  Dave KeaneDave Keane

                                                                  151110




                                                                  151110























                                                                      0














                                                                      This could also be a solution.



                                                                      var isMobile = false; //initiate as false

                                                                      // device detection
                                                                      if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
                                                                      || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(navigator.userAgent.substr(0,4))) isMobile = true;

                                                                      console.log('Mobile device:'+isMobile);

                                                                      var doc_h = $(document).height(); // returns height of HTML document
                                                                      var doc_w = $(document).width(); // returns width of HTML document
                                                                      console.log('Height: '+doc_h);
                                                                      console.log('width: '+doc_w);

                                                                      var iPadVertical = window.matchMedia("(width: 768px) and (height: 1024px) and (orientation: portrait)");
                                                                      var iPadHoricontal = window.matchMedia("(width: 1024px) and (height: 767px) and (orientation: landscape)");

                                                                      console.log('Height: '+doc_h);
                                                                      console.log('width: '+doc_w);

                                                                      if (iPadVertical.matches) {
                                                                      console.log('Ipad vertical detected');
                                                                      }else if (iPadHoricontal.matches){
                                                                      console.log('Ipad horicontal detected');
                                                                      }else {
                                                                      console.log('No Ipad');
                                                                      }


                                                                      If you use both methods, you will get a perfect way to detect different devices.






                                                                      share|improve this answer




























                                                                        0














                                                                        This could also be a solution.



                                                                        var isMobile = false; //initiate as false

                                                                        // device detection
                                                                        if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
                                                                        || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(navigator.userAgent.substr(0,4))) isMobile = true;

                                                                        console.log('Mobile device:'+isMobile);

                                                                        var doc_h = $(document).height(); // returns height of HTML document
                                                                        var doc_w = $(document).width(); // returns width of HTML document
                                                                        console.log('Height: '+doc_h);
                                                                        console.log('width: '+doc_w);

                                                                        var iPadVertical = window.matchMedia("(width: 768px) and (height: 1024px) and (orientation: portrait)");
                                                                        var iPadHoricontal = window.matchMedia("(width: 1024px) and (height: 767px) and (orientation: landscape)");

                                                                        console.log('Height: '+doc_h);
                                                                        console.log('width: '+doc_w);

                                                                        if (iPadVertical.matches) {
                                                                        console.log('Ipad vertical detected');
                                                                        }else if (iPadHoricontal.matches){
                                                                        console.log('Ipad horicontal detected');
                                                                        }else {
                                                                        console.log('No Ipad');
                                                                        }


                                                                        If you use both methods, you will get a perfect way to detect different devices.






                                                                        share|improve this answer


























                                                                          0












                                                                          0








                                                                          0







                                                                          This could also be a solution.



                                                                          var isMobile = false; //initiate as false

                                                                          // device detection
                                                                          if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
                                                                          || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(navigator.userAgent.substr(0,4))) isMobile = true;

                                                                          console.log('Mobile device:'+isMobile);

                                                                          var doc_h = $(document).height(); // returns height of HTML document
                                                                          var doc_w = $(document).width(); // returns width of HTML document
                                                                          console.log('Height: '+doc_h);
                                                                          console.log('width: '+doc_w);

                                                                          var iPadVertical = window.matchMedia("(width: 768px) and (height: 1024px) and (orientation: portrait)");
                                                                          var iPadHoricontal = window.matchMedia("(width: 1024px) and (height: 767px) and (orientation: landscape)");

                                                                          console.log('Height: '+doc_h);
                                                                          console.log('width: '+doc_w);

                                                                          if (iPadVertical.matches) {
                                                                          console.log('Ipad vertical detected');
                                                                          }else if (iPadHoricontal.matches){
                                                                          console.log('Ipad horicontal detected');
                                                                          }else {
                                                                          console.log('No Ipad');
                                                                          }


                                                                          If you use both methods, you will get a perfect way to detect different devices.






                                                                          share|improve this answer













                                                                          This could also be a solution.



                                                                          var isMobile = false; //initiate as false

                                                                          // device detection
                                                                          if(/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
                                                                          || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(navigator.userAgent.substr(0,4))) isMobile = true;

                                                                          console.log('Mobile device:'+isMobile);

                                                                          var doc_h = $(document).height(); // returns height of HTML document
                                                                          var doc_w = $(document).width(); // returns width of HTML document
                                                                          console.log('Height: '+doc_h);
                                                                          console.log('width: '+doc_w);

                                                                          var iPadVertical = window.matchMedia("(width: 768px) and (height: 1024px) and (orientation: portrait)");
                                                                          var iPadHoricontal = window.matchMedia("(width: 1024px) and (height: 767px) and (orientation: landscape)");

                                                                          console.log('Height: '+doc_h);
                                                                          console.log('width: '+doc_w);

                                                                          if (iPadVertical.matches) {
                                                                          console.log('Ipad vertical detected');
                                                                          }else if (iPadHoricontal.matches){
                                                                          console.log('Ipad horicontal detected');
                                                                          }else {
                                                                          console.log('No Ipad');
                                                                          }


                                                                          If you use both methods, you will get a perfect way to detect different devices.







                                                                          share|improve this answer












                                                                          share|improve this answer



                                                                          share|improve this answer










                                                                          answered Mar 18 '16 at 11:45









                                                                          Friis1978Friis1978

                                                                          1,099714




                                                                          1,099714























                                                                              0














                                                                              Here's an ECMAScript 6 solution (TypeScript ready)



                                                                              public isMobile(): boolean {
                                                                              let check = false;
                                                                              ((a => {
                                                                              if (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0, 4))) check = true;
                                                                              }))(navigator.userAgent || navigator.vendor);
                                                                              return check;
                                                                              }





                                                                              share|improve this answer
























                                                                              • why not just return the if condition instead of having this whole check variable?

                                                                                – Vic
                                                                                Apr 20 '18 at 19:15
















                                                                              0














                                                                              Here's an ECMAScript 6 solution (TypeScript ready)



                                                                              public isMobile(): boolean {
                                                                              let check = false;
                                                                              ((a => {
                                                                              if (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0, 4))) check = true;
                                                                              }))(navigator.userAgent || navigator.vendor);
                                                                              return check;
                                                                              }





                                                                              share|improve this answer
























                                                                              • why not just return the if condition instead of having this whole check variable?

                                                                                – Vic
                                                                                Apr 20 '18 at 19:15














                                                                              0












                                                                              0








                                                                              0







                                                                              Here's an ECMAScript 6 solution (TypeScript ready)



                                                                              public isMobile(): boolean {
                                                                              let check = false;
                                                                              ((a => {
                                                                              if (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0, 4))) check = true;
                                                                              }))(navigator.userAgent || navigator.vendor);
                                                                              return check;
                                                                              }





                                                                              share|improve this answer













                                                                              Here's an ECMAScript 6 solution (TypeScript ready)



                                                                              public isMobile(): boolean {
                                                                              let check = false;
                                                                              ((a => {
                                                                              if (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0, 4))) check = true;
                                                                              }))(navigator.userAgent || navigator.vendor);
                                                                              return check;
                                                                              }






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Feb 23 '17 at 8:18









                                                                              0x1ad20x1ad2

                                                                              5,63972638




                                                                              5,63972638













                                                                              • why not just return the if condition instead of having this whole check variable?

                                                                                – Vic
                                                                                Apr 20 '18 at 19:15



















                                                                              • why not just return the if condition instead of having this whole check variable?

                                                                                – Vic
                                                                                Apr 20 '18 at 19:15

















                                                                              why not just return the if condition instead of having this whole check variable?

                                                                              – Vic
                                                                              Apr 20 '18 at 19:15





                                                                              why not just return the if condition instead of having this whole check variable?

                                                                              – Vic
                                                                              Apr 20 '18 at 19:15











                                                                              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:






                                                                              (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                                                                              console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');








                                                                              share|improve this answer




























                                                                                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:






                                                                                (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                                                                                console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');








                                                                                share|improve this answer


























                                                                                  0












                                                                                  0








                                                                                  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:






                                                                                  (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                                                                                  console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');








                                                                                  share|improve this answer













                                                                                  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:






                                                                                  (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                                                                                  console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');








                                                                                  (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                                                                                  console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');





                                                                                  (function(a){window.isMobile = (/(android|bbd+|meego).+mobile|avantgo|bada/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a)||/1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(a.substr(0,4)))})(navigator.userAgent||navigator.vendor||window.opera);

                                                                                  console.info('This is %s device', window.isMoblie ? 'mobile' : 'desktop');






                                                                                  share|improve this answer












                                                                                  share|improve this answer



                                                                                  share|improve this answer










                                                                                  answered Mar 18 '17 at 14:28









                                                                                  SynCapSynCap

                                                                                  2,0862814




                                                                                  2,0862814























                                                                                      0














                                                                                      Depends on the use case. All mobile devices require a battery. If what you're after is compute power without draining the battery use the Battery Status API:



                                                                                      navigator.getBattery().then(battery => {
                                                                                      battery.charging ? 'charging' : 'not charging';
                                                                                      });


                                                                                      If what you're looking for is presentational use matchMedia, which returns a Boolean value:



                                                                                      if (window.matchMedia("(min-width: 400px)").matches) {
                                                                                      /* the viewport is at least 400 pixels wide */
                                                                                      } else {
                                                                                      /* the viewport is less than 400 pixels wide */
                                                                                      }


                                                                                      Or combine them for an even better user experience on tablet devices.






                                                                                      share|improve this answer






























                                                                                        0














                                                                                        Depends on the use case. All mobile devices require a battery. If what you're after is compute power without draining the battery use the Battery Status API:



                                                                                        navigator.getBattery().then(battery => {
                                                                                        battery.charging ? 'charging' : 'not charging';
                                                                                        });


                                                                                        If what you're looking for is presentational use matchMedia, which returns a Boolean value:



                                                                                        if (window.matchMedia("(min-width: 400px)").matches) {
                                                                                        /* the viewport is at least 400 pixels wide */
                                                                                        } else {
                                                                                        /* the viewport is less than 400 pixels wide */
                                                                                        }


                                                                                        Or combine them for an even better user experience on tablet devices.






                                                                                        share|improve this answer




























                                                                                          0












                                                                                          0








                                                                                          0







                                                                                          Depends on the use case. All mobile devices require a battery. If what you're after is compute power without draining the battery use the Battery Status API:



                                                                                          navigator.getBattery().then(battery => {
                                                                                          battery.charging ? 'charging' : 'not charging';
                                                                                          });


                                                                                          If what you're looking for is presentational use matchMedia, which returns a Boolean value:



                                                                                          if (window.matchMedia("(min-width: 400px)").matches) {
                                                                                          /* the viewport is at least 400 pixels wide */
                                                                                          } else {
                                                                                          /* the viewport is less than 400 pixels wide */
                                                                                          }


                                                                                          Or combine them for an even better user experience on tablet devices.






                                                                                          share|improve this answer















                                                                                          Depends on the use case. All mobile devices require a battery. If what you're after is compute power without draining the battery use the Battery Status API:



                                                                                          navigator.getBattery().then(battery => {
                                                                                          battery.charging ? 'charging' : 'not charging';
                                                                                          });


                                                                                          If what you're looking for is presentational use matchMedia, which returns a Boolean value:



                                                                                          if (window.matchMedia("(min-width: 400px)").matches) {
                                                                                          /* the viewport is at least 400 pixels wide */
                                                                                          } else {
                                                                                          /* the viewport is less than 400 pixels wide */
                                                                                          }


                                                                                          Or combine them for an even better user experience on tablet devices.







                                                                                          share|improve this answer














                                                                                          share|improve this answer



                                                                                          share|improve this answer








                                                                                          edited Nov 22 '18 at 12:39

























                                                                                          answered Nov 22 '18 at 12:22









                                                                                          Josh HabdasJosh Habdas

                                                                                          2,8672532




                                                                                          2,8672532























                                                                                              -1














                                                                                              If you're looking for a mobile phone, since they're portrait oriented in most cases, you can use this simple check:



                                                                                              if ($(window).width() > $(window).height())
                                                                                              alert('Landscape'); // usually tablet or desktop
                                                                                              else
                                                                                              alert('Portrait'); // usually mobile phone


                                                                                              NOTE: this is not a reliable solution, but it will work in most cases!






                                                                                              share|improve this answer






























                                                                                                -1














                                                                                                If you're looking for a mobile phone, since they're portrait oriented in most cases, you can use this simple check:



                                                                                                if ($(window).width() > $(window).height())
                                                                                                alert('Landscape'); // usually tablet or desktop
                                                                                                else
                                                                                                alert('Portrait'); // usually mobile phone


                                                                                                NOTE: this is not a reliable solution, but it will work in most cases!






                                                                                                share|improve this answer




























                                                                                                  -1












                                                                                                  -1








                                                                                                  -1







                                                                                                  If you're looking for a mobile phone, since they're portrait oriented in most cases, you can use this simple check:



                                                                                                  if ($(window).width() > $(window).height())
                                                                                                  alert('Landscape'); // usually tablet or desktop
                                                                                                  else
                                                                                                  alert('Portrait'); // usually mobile phone


                                                                                                  NOTE: this is not a reliable solution, but it will work in most cases!






                                                                                                  share|improve this answer















                                                                                                  If you're looking for a mobile phone, since they're portrait oriented in most cases, you can use this simple check:



                                                                                                  if ($(window).width() > $(window).height())
                                                                                                  alert('Landscape'); // usually tablet or desktop
                                                                                                  else
                                                                                                  alert('Portrait'); // usually mobile phone


                                                                                                  NOTE: this is not a reliable solution, but it will work in most cases!







                                                                                                  share|improve this answer














                                                                                                  share|improve this answer



                                                                                                  share|improve this answer








                                                                                                  edited Sep 27 '17 at 11:09

























                                                                                                  answered Sep 17 '17 at 9:54









                                                                                                  vooxovooxo

                                                                                                  4514




                                                                                                  4514






















                                                                                                      1 2
                                                                                                      next


                                                                                                      Popular posts from this blog

                                                                                                      Homophylophilia

                                                                                                      Updating UILabel text programmatically using a function

                                                                                                      Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage