Skip to content

Instantly share code, notes, and snippets.

@kevnk

kevnk/isMobile.coffee

Last active Mar 15, 2017
Embed
What would you like to do?
Test for mobile via coffeescript
Site = ->
# @see: http://stackoverflow.com/a/20413768/622287
_isHighDensity: ->
((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 124dpi), only screen and (min-resolution: 1.3dppx), only screen and (min-resolution: 48.8dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (min-device-pixel-ratio: 1.3)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 1.3))
_isRetina: ->
((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 2)) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent)
_checkMobile:
webOS: ->
/webOS/i.test(navigator.userAgent)
Android: ->
/Android/i.test(navigator.userAgent)
BlackBerry: ->
/BlackBerry/i.test(navigator.userAgent)
iOS: ->
/iPhone|iPad|iPod/i.test(navigator.userAgent)
Opera: ->
/Opera Mini/i.test(navigator.userAgent)
Windows: ->
/IEMobile/i.test(navigator.userAgent)
isMobile: ->
isMobile = false
_.each @_checkMobile, (func, key) ->
isMobile = key if func() is true
isMobile
# Usage
site = new Site()
site.isMobile() # false or (string) e.g. 'iOS'
var Site, site;
Site = function() {
return {
_isHighDensity: function() {
return (window.matchMedia && (window.matchMedia('only screen and (min-resolution: 124dpi), only screen and (min-resolution: 1.3dppx), only screen and (min-resolution: 48.8dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 1.3), only screen and (-o-min-device-pixel-ratio: 2.6/2), only screen and (min--moz-device-pixel-ratio: 1.3), only screen and (min-device-pixel-ratio: 1.3)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 1.3);
},
_isRetina: function() {
return ((window.matchMedia && (window.matchMedia('only screen and (min-resolution: 192dpi), only screen and (min-resolution: 2dppx), only screen and (min-resolution: 75.6dpcm)').matches || window.matchMedia('only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (-o-min-device-pixel-ratio: 2/1), only screen and (min--moz-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2)').matches)) || (window.devicePixelRatio && window.devicePixelRatio > 2)) && /(iPad|iPhone|iPod)/g.test(navigator.userAgent);
},
_checkMobile: {
webOS: function() {
return /webOS/i.test(navigator.userAgent);
},
Android: function() {
return /Android/i.test(navigator.userAgent);
},
BlackBerry: function() {
return /BlackBerry/i.test(navigator.userAgent);
},
iOS: function() {
return /iPhone|iPad|iPod/i.test(navigator.userAgent);
},
Opera: function() {
return /Opera Mini/i.test(navigator.userAgent);
},
Windows: function() {
return /IEMobile/i.test(navigator.userAgent);
}
},
isMobile: function() {
var isMobile;
isMobile = false;
_.each(this._checkMobile, function(func, key) {
if (func() === true) {
return isMobile = key;
}
});
return isMobile;
}
};
};
site = new Site();
site.isMobile();
// ---
// generated by coffee-script 1.9.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment