Created
February 26, 2014 09:48
-
-
Save msenkpiel/9226752 to your computer and use it in GitHub Desktop.
Javascript Mobile Utils
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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