Wednesday, 20 March 2013

Javascript to detect mobile browser

Javascript to detect mobile browser....

put this javascript on page and it will detect that the device is mobile,tablet and ipad or not.


<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(you have opened page in mobile, tablet or ipad.);
                   //following line is to open mobile version of page.
window.location = "http://www.test.com/mobile"
  }else{
                    alert('You are using a desktop browser to view this page..');
}

</script>

No comments:

Post a Comment