I keep jumping into projects where Modernizr is located in the head, but it isn't being used (at least for feature detection). Now, I love Modernizr, I use it frequently for feature detection and fallbacks on projects that require it; however, the last three projects I've come into it's been sitting in the head without any of the feature detection classes being called. These projects are using vanilla javascript and/or do not require jQuery polyfills at all. 1 That being said... if you're not using the feature detection and really don't need to load a jQuery library 2, is Modernizr really doing anything aside from making an addition HTTP request and resource to load?
I'm not strong enough with jQuery/javascript to understand if it's affecting anything else under the hood.
Edit
1 & 2 — Modernizr is javascript and doesn't require the jQuery library (which makes me wonder why the jQuery library is being loaded also, at least in these cases).
Modernizr.min with only #-shiv-cssclasses-load
is 7.57 KB whereas html5shiv.min is only 3 KB.
Modernizr is a JavaScript library that allows you to use HTML5 / CSS3 without having to accept that the site is not working properly in older browsers. Modernizr checks every requesting browser for what it can and can not do. The result of this check is stored in a specially created JavaScript object.
Answer: For feature detection, Modernizr performs three basic functions: Adds classes indicating feature support, which can be used to conditionally apply CSS styling rules to different elements. Creates a JavaScript object to check or validate support for any HTML or CSS feature in a browser.
We can access various properties of this object 'Modernizr' for feature detection using “Modernizr. featureName”. For example, Modernizr. video will return “true” if the browser supports the video element, and false if the browser doesn't.
Generally speaking, Modernizr does three things.
If you don't use any of these features, then there's no point in including Modernizr at all. If you only use it for the html5shiv then you could probably just include that instead to save a few bytes, although I doubt there's a relevant size difference at all.
That said, you should never include feature detects in a modernizr build that you don't use. That's nothing but a waste.
Modernizr also include shims which save you from defining them yourself or including another library.
The main goal of Modernizr is to use detection via classes. so you can do something like this:
Example, where you want to change behaviour depending on whether the client support flash:
.flash #flashdiv { display:block; } .no-flash #flashdiv { display:none; }
It really helps you to dynamically deal with the abilities different client browsers.
Also, you can use Modernizr to detect HTML5 features and provide fallbacks (polyfills).
Eg:
<script> if (Modernizr.canvas) { alert("This browser supports HTML5 canvas!"); //you can load js here. or use yepnope) } </script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With