Skip to content

Instantly share code, notes, and snippets.

@msenkpiel
Created February 26, 2014 09:48
Show Gist options
  • Save msenkpiel/9226752 to your computer and use it in GitHub Desktop.
Save msenkpiel/9226752 to your computer and use it in GitHub Desktop.
Javascript Mobile Utils
App.utils = {
mobile:{
isAndroid: function() {
return navigator.userAgent.match(/Android/i);
},
isBlackBerry: function() {
return navigator.userAgent.match(/BlackBerry/i);
},
isIos: function() {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
isOpera: function() {
return navigator.userAgent.match(/Opera Mini/i);
},
isWindows: function() {
return navigator.userAgent.match(/IEMobile/i);
},
isMobileDevice: function() {
return (this.isAndroid() || this.isBlackBerry() || this.isIos() || this.isOpera() || this.isWindows());
},
isPortrait:function(){
return ($(window).width() < $(window).height());
},
isLandscape:function(){
return ($(window).width() > $(window).height());
},
isSmallScreen:function(){
return ($(window).width() < 768);
},
check:function(){
var $meta = $('meta[name="viewport"]');
var $body = $('body');
var $hiddenMobile = $('.hidden-mobile');
var $visibleMobile = $('.visible-mobile');
$meta.attr('content','initial-scale=1, width=device-width, user-scalable=1');
$body.removeClass('mobile-device');
//$hiddenMobile.show();
//$visibleMobile.hide();
if(this.isSmallScreen()){
$meta.attr('content','initial-scale=1, width=device-width, user-scalable=0');
$body.addClass('mobile-device');
//$hiddenMobile.hide();
//$visibleMobile.show();
}
},
init:function(){
var scope = this;
$(window).on('resize orientationchange', function (e) {
scope.check();
});
// initial call
scope.check();
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment