Skip to content

Instantly share code, notes, and snippets.

@zdying
Created April 3, 2015 15:11
Show Gist options
  • Save zdying/fc1343d1327ad89fdf18 to your computer and use it in GitHub Desktop.
Save zdying/fc1343d1327ad89fdf18 to your computer and use it in GitHub Desktop.
device
(function(win) {
var userAgent = win.navigator.userAgent.toLowerCase ();
win.device = {
'ipod': function(){
return test(/ipod/)
},
'ipad': function(){
return test(/ipad/)
},
'iphone': function() {
return test(/iphone/)
},
'ios' : function(){
return test(/(ipod|ipad|iphone)/)
},
'android': function(){
return test(/android/)
},
'androidPhone': function(){
return this.android() && test(/mobile/)
},
'androidTablet': function(){
return this.android() && !test(/mobile/)
},
'blackberry': function(){
return test(/(blackberry|bb10|rim)/)
},
'blackberryPhone': function(){
return this.blackberry() && !test(/tablet/)
},
'blackberryTablet': function(){
return this.blackberry() && test(/tablet/)
},
'windows': function(){
return test(/windows/)
},
'windowsPhone': function(){
return this.windows() && test(/phone/)
},
'windowsTablet': function(){
return this.windows() && test(/touch/)
},
'mobile' : function(){
return this.ipod() || this.iphone() || this.androidPhone() || this.blackberryPhone() || this.windowsPhone()
},
'tablet' : function(){
return this.ipad() || this.androidTablet() || this.blackberryTablet() || this.windowsTablet()
},
'type' : function(){
return device.mobile() ? 'mobile' : device.tablet() ? 'tablet' : 'desktop'
},
'os' : function(){
return this.ios() ? 'ios' :
this.android() ? 'android' :
this.windows() ? 'windows' :
this.blackberry() ? 'blackberry' : 'other'
}
};
function test(reg){
return reg.test(userAgent)
}
//console.warn('Current device :', device.type())
//console.warn('Current os :', device.os())
})(window);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment