Wednesday 28 September 2011

Detecting Mobile Browser using JavaScript

It is very simple to detect that visitor is accessing your site from Mobile browser or from Desktop browser.
Just use following JavaScript,copy and paste it into your web page.Almost all Mobile Devices are supported by this code.

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<script type="text/javascript">
         var mobile = function () {
             return {
                 detect: function () {
                     var uagent = navigator.userAgent.toLowerCase();
                     var list = this.mobiles;
                     var ismobile = false;
                     for (var d = 0; d < list.length; d += 1) {
                         if (uagent.indexOf(list[d]) != -1) {
                             ismobile = true;
                         }
                     }
                     return ismobile;
                 },
                 mobiles: [
    "midp", "240x320", "blackberry", "netfront", "nokia", "panasonic",
    "portalmmm", "sharp", "sie-", "sonyericsson", "symbian",
    "windows ce", "benq", "mda", "mot-", "opera mini",
    "philips", "pocket pc", "sagem", "samsung", "sda",
    "sgh-", "vodafone", "xda", "palm", "iphone",
    "ipod", "android"
   ]
             };
         } ();

        if (mobile.detect()) {
             alert('This is Mobile Page !!');
         } else {
            alert('This is Desktop Browser Page !!');
         }
</script>
</head>
<body>

<h1>This is a Heading</h1>
<p>This is a paragraph.</p>

</body>
</html>

 

No comments:

Post a Comment