Skip to content

Instantly share code, notes, and snippets.

@travismillerweb
Created March 30, 2014 17:03
Show Gist options
  • Save travismillerweb/9875968 to your computer and use it in GitHub Desktop.
Save travismillerweb/9875968 to your computer and use it in GitHub Desktop.
JS - Match Media Function
/*
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