Skip to content

Instantly share code, notes, and snippets.

@brianlmoon
Last active December 13, 2015 19:28
Show Gist options
  • Save brianlmoon/4962688 to your computer and use it in GitHub Desktop.
Save brianlmoon/4962688 to your computer and use it in GitHub Desktop.
Here is a function to return you the width the browser is using for media queries. For some reason, it is a bit different in each browser.
function getWindowMediaWidth() {
if(!window.matchMedia){
// it should be this, so return it when we can't
// figure it out. Of course, it does not do a lot
// of good if the browser does not support media
// queries.
return document.body.clientWidth;
}
// it should be one of these values starting with the
// first one that makes the most sense
var values = [
document.body.clientWidth, // Chrome returns this
window.innerWidth, // Firefox returns this because
window.outerWidth, // this is the same as above on browsers with no window borders
screen.width // last ditch effort here
];
for(var x = 0; x<values.length; x++){
if (window.matchMedia("(min-width: " + values[x] + "px) and (max-width: " + values[x] + "px)").matches) {
return values[x];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment