Skip to content

JavaScript detect mobile device browser | HTML Example code

  • by

HTML webpage can detect mobile devices using JavaScript. Use JavaScript with regular expression tests to detect if a browser is a mobile device or not.

Example of JavaScript detect mobile device browser

Here’s a code that uses an insanely long and comprehensive regex that returns a true or false value depending on whether or not the user is browsing with a mobile.

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
        const userAgent = navigator.userAgent.toLowerCase();

        var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
        if(isMobile){
          alert("mobile device");
      }else{

          alert("not mobile device");
      }

  </script>
</head>
<body>

</body>
</html>

Output:

JavaScript detect mobile device browser

Do comment if you have another better way or doubts or suggestion on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *