How to Detect a Mobile Device using jQuery

Introduction

With the increasing popularity of mobile devices, it has become essential for web developers to create responsive websites that adapt to different screen sizes. One common requirement is the ability to detect whether a user is accessing the website from a mobile device or not. In this article, we will explore various techniques to detect a mobile device using jQuery.

Method 1: Checking the User Agent string

One of the simplest ways to detect a mobile device is by checking the User Agent string of the browser. The User Agent string contains information about the browser, operating system, and device being used. In JavaScript, we can access the User Agent string using the navigator.userAgent property.


            if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)){
                // Code to run for mobile devices
            }
            else{
                // Code to run for desktop devices
            }
        

In the above code snippet, we use a regular expression to check if the User Agent string matches any of the commonly used mobile device patterns. If a match is found, we can assume that the user is accessing the website from a mobile device and execute the corresponding code.

Method 2: Screen width and height

Another approach to detect a mobile device is by checking the screen width and height. Most mobile devices have smaller screen sizes compared to desktop devices. We can use the screen.width and screen.height properties to get the dimensions of the user's screen.


            if(screen.width <= 768 && screen.height <= 1024){
                // Code to run for mobile devices
            }
            else{
                // Code to run for desktop devices
            }
        

In the above code snippet, we compare the screen width and height with typical mobile device dimensions. If the screen dimensions are smaller or equal to 768 pixels wide and 1024 pixels high, we can assume that the user is using a mobile device.

Method 3: Using a Mobile Device Detection Library like MobileDetect.js

If you prefer a more robust and comprehensive solution, you can use a mobile device detection library like MobileDetect.js. MobileDetect.js is a lightweight JavaScript library that provides an easy-to-use API to detect various attributes of the user's device, including whether it is a mobile device or not.

To use MobileDetect.js, you need to include the library in your HTML file:


            <script src="https://cdnjs.cloudflare.com/ajax/libs/MobileDetect/1.4.4/MobileDetect.min.js"></script>
        

Once the library is included, you can use it to detect mobile devices:


            var md = new MobileDetect(window.navigator.userAgent);

            if(md.mobile()){
                // Code to run for mobile devices
            }
            else{
                // Code to run for desktop devices
            }
        

Conclusion

In this article, we have explored different techniques to detect a mobile device using jQuery. We have seen how to check the User Agent string, screen width and height, and how to use a mobile device detection library like MobileDetect.js. Depending on your requirements and the level of accuracy you need, you can choose the method that best suits your needs.