Created
March 30, 2014 17:03
-
-
Save travismillerweb/9875968 to your computer and use it in GitHub Desktop.
JS - Match Media Function
This file contains 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
/* | |
JS - Media Match Function | |
Use JavaScript's matchMedia Method to detect the type of device you are using, and invoke/activate functions based on those criteria. | |
Reference Links: | |
http://davidwalsh.name/orientation-change | |
http://davidwalsh.name/device-state-detection-css-media-queries-javascript | |
http://www.slideshare.net/DavidKnight5/howto-match-media-25008199 | |
http://caniuse.com/matchmedia | |
*/ | |
var design; | |
if (window.matchMedia('only screen and (max-device-width: 320px)').matches) { | |
design = 'touch'; | |
} else if (window.matchMedia('only screen and (max-device-width: 1024px)').matches) { | |
design = 'tablet'; | |
} else if (window.matchMedia('screen').matches) { | |
design = 'desktop'; | |
} else if (window.matchMedia('handheld').matches) { | |
design = 'mobile'; | |
} | |
console.log(design); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment