How to Detect Mobile Browsers With JavaScript


Mobile phone users do not have the bandwidth and transfer speed to load Web pages with large graphics and text. Website owners detect mobile phone browsers to forward the user to a section of the website optimized for mobile browsers. You can detect a mobile phone browser using JavaScript that loads when the user opens the website.

Instructions

    • 1
      Create the script tags that contain the JavaScript browser detection. Type the following tags within the body of the HTML code of the website:
      <script type="text/javascript"> </script>

    • 2
      Create the variable to assign the user's browser name. Type the following code to create a JavaScript variable:
      var browser;

    • 3
      Detect the mobile phone browser and redirect accordingly. The browser type is contained in the "user agent" function sent by the user's browser. The following code reads the user agent value:
      browser = navigator.userAgent.toLowerCase();

    • 4
      Redirect the user to a mobile section of your website. For instance, the following code redirects the user if the "iPhone" browser is detected:
      if (browser == 'iphone') {
      window.location = 'mobile/index.php';
      }



1 Comments
Disqus
Fb Comments
Comments :

1 comment:

  1. Hello,

    There are many more browsers and more incompatibilities on the mobile platforms than on the desktop. After trying to build portable websites for mobile browsers. Detect the mobile phone browser and redirect accordingly. The browser type is contained in the user agent function sent by the user's browser. Thanks..

    Mobile Survey

    ReplyDelete