Sometimes we need a little Javascript code snippet to detect if user use mobile device, the simplest way is detecting its browser user agent.
We use the regular expression test to detect if browser is a mobile device like:
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
// true for mobile device
document.write("mobile device");
}else{
// false for not mobile device
document.write("not mobile device");
}
demo is on codepen:
https://codepen.io/timhuang/pen/MWKEZMJ
Reference:
https://stackoverflow.com/questions/3514784/what-is-the-best-way-to-detect-a-mobile-device
Latest comments (34)
I am using a mobile and when I open the TNT Sim registration official URL then my mobile creates different issues.
'ontouchstart' in window=> true for mobile devices
=> false for desktop environment
Some desktops have touchscreen devices, this is not reliable
I think you've taken the question too literally. The main issue behind the question is for developers to distinguish whether their JavaScript code needs to handle touch events vs click events, and not to figure out if someone can literally lug their device around in a mobile manner. Determining if touch events are in the window object is a simple way to determine this. Cheers!
No, this is very often not true.
In my case, I'm interested in this right now because I need to default the UI rendering of a webpage to relate better to our mobile app when the user is on mobile.
If the user lands on a marketing page, for example, we want to frontload the marketing copy that talks about the mobile features of our product suite. That's not relevant to someone running Windows on a touchscreen laptop.
it's better to use 'navigator.userAgentData.mobile' because it only returns true or false
unfortunately this property is not supported all browsers
developer.mozilla.org/en-US/docs/W...
This is working fine! Thank you.
Great! I hope this could help who need to detect if browser on a mobile device.
Thanks for your visiting.
Alternatively, you can use
navigator.mediaDevices.enumerateDevices().then(md => { console.log(md) }); and use field
MediaDeviceInfo.kind Read only
Returns an enumerated value that is either "videoinput", "audioinput" or "audiooutput".
and
MediaDeviceInfo.groupId Read only
Returns a DOMString that is a group identifier. Two devices have the same group identifier if they belong to the same physical device — for example a monitor with both a built-in camera and a microphone.
That is, if several "videoinput" and their groupId are the same, most likely this is a mobile device, since there are more than one laptop and monitor with two cameras and more.
That function suported all desktop and mobile browsers except IE.
Great! An alternative solution. Thanks!
User Agent detection is not a recommended technique for modern web apps. There is a JavaScript API built-in for detecting media. The JavaScript window.matchMedia() method returns a MediaQueryList object representing the results of the specified CSS media query string. You can use this method to detect a mobile device based on the CSS media query.
$(document).ready(function(){
let isMobileDevice = window.matchMedia("only screen and (max-width: 760px)").matches;
if(isMobileDevice){
// The viewport is less than 768 pixels wide
//Conditional script here
} else{
//The viewport is greater than 700 pixels wide
alert("This is not a mobile device.");
}
});
You can use above script to do show/hide elements depending on the screen size.
net-informations.com/js/progs/mobi...
You can use JavaScript window.matchMedia() method to detect a mobile device based on the CSS media query.
if (window.matchMedia("(max-width: 767px)").matches)
{
// The viewport is less than 768 pixels wide
document.write("This is a mobile device.");
}
You may also use navigator.userAgentData.mobile .
const isMobile = navigator.userAgentData.mobile;
Another approach would be a responsive media query. You could presume that a mobile phone has a screen size greater than x and less than y. For example:
@media only screen and (min-width: 320px) and (max-width: 600px) {}
net-informations.com/js/iq/default...
Unfortunately navigator.userAgentData.mobile appears to be not widely supported yet
caniuse.com/?search=userAgentData
caniuse.com/?search=mobile
Thanks for sharing!
It worked very well for me. Thanks, @timhuang
I adapted the modification proposed by @drgaud
an interesting solution for new browsers, and most importantly fast:
stackoverflow.com/questions/783868...?
a pity that display media-queries are not available for very ancient devices and browsers (hello Safari)
for server side detect device in production, i use packet npmjs.com/package/node-device-dete... (I'm the author, maybe I'm PR late)
Thank you!
I have made some improvements to the speed of detecting devices and clients, if you also use the library, I recommend updating the dependency to the latest version.
Great! Thank you!
hello,
thank man,
it helped me a lot. working like a charm.