Skip to content

Instantly share code, notes, and snippets.

@kmaida
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmaida/9348846 to your computer and use it in GitHub Desktop.
Save kmaida/9348846 to your computer and use it in GitHub Desktop.
Function to check if matchMedia is supported; if so, use it, otherwise, use JS window width calculation (fixes issues with -webkit browsers returning incorrect window widths due to scrollbars)
function getviewport() {
var win = window,
matchMediaSupported = (win.matchMedia || win.msMatchMedia),
viewport;
if (matchMediaSupported) {
if (win.matchMedia('screen and (min-width: 641px)').matches) {
viewport = 'large';
} else {
viewport = 'small';
}
} else if (!matchMediaSupported && win.width > 640) {
viewport = 'large';
} else {
viewport = 'small';
}
return viewport;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment