How to Detect a Mobile Device in jQuery
Answer: Use the JS
window.matchMedia() method to detect a mobile device based on the CSS media query. This is the best and most reliable way to detected mobile devices.
The following example will show you how this method actually works:
ExampleTry this code »
// The viewport is less than 768 pixels wide
alert("This is a mobile device.");
// The viewport is at least 768 pixels wide
alert("This is a tablet or desktop.");
matchMedia() method is supported in all major modern browser such as Chrome, Firefox, Internet Explorer (version 10 and above), and so on.
Here are some more FAQ related to this topic: