How to Detect a Mobile Device in jQuery
In this tutorial, we are going to see how to detect a mobile device in jQuery. You can simply use the window.matchMedia() method of JavaScript to detect a mobile device based on media requests in CSS. It is the most reliable way to detect mobile devices.
The following example will show you how this method actually works:
How to Detect a Mobile Device in jQuery
$(document).ready(function(){ if(window.matchMedia("(max-width: 600px)").matches){ // Window is less than 600 pixels wide alert("This is a mobile device."); } else{ // Window is at least 600 pixels wide alert("This is a tablet or desktop."); } });