EDIT: I'm no longer curating this answer as I feel that Niet's is much better (see chosen answer). It should still work, but may need some testing.
Cracking open Modernizr (which can test for media queries) we see it uses the window.matchMedia
(MDN page) function. We can check in plain old vanilla JS to see if this function exists:
function mediaQueriesEnabled () {
if(typeof window.matchMedia != "undefined" || typeof window.msMatchMedia != "undefined") {
return true;
} else {
return false;
}
}
Or more elegantly:
function mediaQueriesEnabled () {
return (typeof window.matchMedia != "undefined" || typeof window.msMatchMedia != "undefined");
}
JSFiddle
I've tested in the following browsers that support media queries, all correctly returned true:
I've tested in the following browsers that do not support media queries, all correctly returned false:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…