April 12, 2021

What is Modernizr

Modernizr is a small piece of JavaScript code library for feature detection that detects the browser support next-generation web technologies in your user’s browsers. Modernizr uses feature detection to allow you to easily fit your website on user’s experiences based with the actual capabilities of their browser.

There are several JavaScript libraries are available now a days, which can help you detect features and browsers. Using these libraries we can detect whether the browser has support for that particular feature or not.

Modernizr gives the ability to detect the new features in the browsers that can render or utilize

Let’s take a example with plain javascript to detect canvas feature.

In this code, we tried to create a canvas element and then added it to body.

we could use the Modernizr.canvas property to test if the browser supported canvas or not:

Modernizr creates an object named Modernizr to the document, which contains all of the test results as Boolean properties. We can write a simple script to list all supported and unsupported features:

Polyfilling with YepNope,requireJS

As a Developer, we can load the JS as per the feature is supported by your browsers. We can use Modernizr.load() method to test if a certain feature is supported or not and load the appropriate JavaScript based on the result. Unfortunately Modernizr.load() is not provided in the modernizr.js file by default and was a part of Modernizr Version 2.8.3. But there are many other third party libraries are available to load resources like YepNope,requireJS,HeadJS etc.

Modernizr.load() is actually yepnope.js (http://yepnopejs.com/). yepnope.js was known as a conditional loader for polyfills, but now its deprecated after version 1.5.

Let’s take a example for this:

In below code we are checking if touch is available in the browser then it will load touch.js, otherwise it will load the no-touch.js file into the browser.

For the newer versions of Modernizr, we can include yepnope.js to use Modernizr.load().

Developers can also wrote the previous example of code as below with RequireJS:

Leave a Reply

Your email address will not be published. Required fields are marked *