I'm trying to detect whether media queries are present using Modernizr 2, then loading in respond.js if appropriate.
I've put this in my script.js file...
Modernizr.load({
test: Modernizr.mq,
yep : '',
nope: 'mylibs/respond.js'
});
What am I doing wrong? I'm really surprised there isn't an example of how to do this with Media Queries on the Modernizr site. Here is the version of Modernizr I'm using...
That's because
!!Modernizr.mq === true
at all times... you're testing for the wrong thing!As per the docs:
But this:
Modernizr.mq()
isfalse
too! You have to actually test for something. Here, theall
keyword is just what you need (oronly all
as Paul suggests):However, all custom builds of Modernizr 2.0.x with
mq
include respond.js, so you never really need to test this, except if you want to load another polyfill instead. In that case, you will need to disable/remove respond.js from your build.Modernizr 2.5.x
With the arrival of Modernizr 2.5.x, the above is no longer true. The abbreviated changelog specifies that:
This means
Modernizr.mq('only all')
may now returnfalse
...Just noticed this conclusion is reached in the comments of Felix's answer - I'm leaving my answer here in case it helps other visitors who, like me, did not get it.
Not sure if this is still an issue but if you are loading Modernizr v2.0.6 you shouldn't need to run this test. Just adding it to your page should automatically fire-up respond.js and your media queries should start working.
I've been scratching my head over this as well since I kept getting "true" returned in IE8!! Poorly explained on the Modernizr site but alluded to in the http://html5boilerplate.com/ (first instance)