JavaScript - Determine if user is on mobile device

You can use navigator.userAgent value to determine if the user is on a mobile device.

commit 9d34c3b · authored · 1 min read

JavaScript - Determine if user is on mobile device

In the responsive design era, you may want to do some extra operations if the user is on mobile, like something I mentioned in my last post where my colleague wanted to remove some specific classes if the user is on mobile.

To do that, you can check the navigator.userAgent value. And with the help of regex you can do,

var isMobile = /android.+mobile|ip(hone|[oa]d)/i.test(navigator.userAgent);

As you can see, we are matching against few of the major mobile user agents. But keep in mind that there are too many devices variants so user agents too, so if you need a more accurate result may be this answer on SO will be helpful.

local graph · open full graph

"JavaScript - Determine if user is on mobile device" is connected to the topics JavaScript and related to 6 other entries. Vue.js List Rendering : Limit items in v-for Vue.js List Rendering : Lim… Remove specific class from all elements - jQuery Remove specific class from … Get/Set hidden field value using jQuery Get/Set hidden field value … Set selected option by value via jQuery Set selected option by valu… Decode JWT Token Decode JWT Token Domain Name From URL Domain Name From URL JavaScript #javascript JavaScript - Determine if user is…

comments

loading discussion…