[Drupal] How to detect the mobile browser using JavaScript?

| | 1 min read

While accesing site from mobile it needs to be changed as per the mobile reqirement. So we need to check whether the site is accessing from mobile devices or from the desktop or laptop. There is an easy way to detect it witht help of Javascript.

  • To get the platform
          navigator.platform
        
  • To get the browser application
          navigator.appName
        
  • Example
        
          if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
          // do something
          }
        
      

As by the above example we can easily findout the mobile phones by the operating systems used in the mobile