Skip to content

Instantly share code, notes, and snippets.

@manishsongirkar
Last active August 29, 2015 14:07
Show Gist options
  • Save manishsongirkar/e433c73c6e9ffcbefe2b to your computer and use it in GitHub Desktop.
Save manishsongirkar/e433c73c6e9ffcbefe2b to your computer and use it in GitHub Desktop.
Check UserAgent and apply class to body tag
jQuery( document ).ready( function() {
var isMobile = {
Android: function() {
return navigator.userAgent.match( /Android/i );
},
BlackBerry: function() {
return navigator.userAgent.match( /BlackBerry/i );
},
iOS: function() {
return navigator.userAgent.match( /iPhone|iPad|iPod/i );
},
iphone: function() {
return navigator.userAgent.match( /iPhone/i );
},
ipad: function() {
return navigator.userAgent.match( /iPad/i );
},
ipod: function() {
return navigator.userAgent.match( /iPod/i );
},
Opera: function() {
return navigator.userAgent.match( /Opera Mini/i );
},
Windows: function() {
return navigator.userAgent.match( /IEMobile/i );
},
any: function() {
return ( isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows() );
}
};
if ( isMobile.any() !== null ) {
jQuery( 'body' ).addClass( 'mobile-device' );
}
if ( isMobile.iphone() !== null ) {
jQuery( 'body' ).addClass( 'iphone' );
}
if ( isMobile.ipad() !== null ) {
jQuery( 'body' ).addClass( 'ipad' );
}
if ( isMobile.ipod() !== null ) {
jQuery( 'body' ).addClass( 'ipod' );
}
if ( isMobile.iOS() !== null ) {
jQuery( 'body' ).addClass( 'ios-device' );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment