Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Modernizr really needed if you're not using the feature detection?

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.

like image 317
darcher Avatar asked Feb 02 '14 14:02

darcher


People also ask

Why do I need modernizr?

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.

What are the features detected by modernizr web workers?

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.

How does modernizr detect 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.


2 Answers

Generally speaking, Modernizr does three things.

  1. It adds classes indicating feature support, allowing you to apply different styling to elements depending on what features they support.
  2. It allows you to run feature detection to decide whether to run a script/run a polyfill or not.
  3. It injects html5shiv, which allows old browsers to understand HTML5 elements.

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.

like image 115
Nils Kaspersson Avatar answered Sep 24 '22 15:09

Nils Kaspersson


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> 
like image 29
Royi Namir Avatar answered Sep 25 '22 15:09

Royi Namir