Monday 5 September 2011

Detect Browser or User Agent from Client-side using JavaScript

Optimized or tweaked your website for particular device platform, such as iPhone, Android, Windows 7 and etc., becomes more and more critical for improving user experience and attracting more traffic to your website. Usually it makes more sense to detect user browser or user agent on server side in order to redirect traffic to your well built device specific version of your main site as well as regular PC or Mac users. But there is always a case that client-side detection is more handy. Furthermore, it's very easy to do it using JavaScript and is supported by almost every browser platform.

 

<script type="text/javascript">
function doDevice () {
   var agent = navigator.userAgent;
   if (agent.indexOf('Windows') > 0) {
       // it's windows platform
   } else if (agent.indexOf('Android') > 0){
      // it's android platform
   } else if (agent.indexOf('iPhone') > 0) {
      // it's iPhone platform
  }
}
</script>

 

No comments:

Post a Comment