Figure3
Adding a reference to
Modernizr in your pages supplies four major features:
-
A comprehensive list
of features supported that's cleverly added to your markup, which enables
conditional CSS definitions.
-
A JavaScript object
that aids in script-based feature detection.
-
All of the new HTML5
tags added to the DOM at run time, for the benefit of Internet Explorer 8
and previous Internet Explorer browsers (more on that in a moment).
-
A script loader for conditionally loading polyfills into your pages.
Polyfilling
Modernizr, one of the great tools for feature detection, is considered a polyfill. It helps detect browser features and only apply shims to only browsers that lack the features. Without changing code, you are able to apply shims and polyfills as needed.
The name Modernizr might throw you for a
second, we'll admit. The library does allow you to use the new HTML5 sectioning elements in IE, but aside from that, it doesn't modernize any other features.
The name Modernizr actually stems from the goal of modernizing our development practices (and ourselves). However! Modernizr still pairs extremely well with scripts that do provide support when native browser support is lacking. In general, these scripts are called polyfills.
polyfill (n)- A JavaScript shim that replicates the standard API for older browsers. Standard API refers to a
given HTML5 technology or feature, like canvas.
Using Modernizr to Polyfill Canvas Support
- Modernizr.load({
- test: Modernizr.canvas,
- nope: '../js/excanvas.js',
- complete: function () {
- Modernizr.load('../js/html5CanvasLogo.js');
- }
- }]);
Here, I'm using the Modernizr script loader to specify three things.
- A Boolean expression to test
- A path to a script to load if the
expression evaluates to false
- A callback to run after the check or
script loading is complete
In the context of canvas, this is all I need to
add some intelligence and polyfilling to my application. Modernizr will
asynchronously load excanvas.js only for browsers that don't support canvas, and
then will load my script library for drawing the HTML5 logo on the page.
Using Modernizr and PIE to Add CSS3 Support
- Modernizr.load({
- test: Modernizr.borderradius || Modernizr.boxshadow,
- nope: '../js/PIE.js',
- callback: function () {
- $('article').each(function () {
- PIE.attach(this);
- });
- }
- });
Using Polyfills to Aid in Graceful
Degradation
Graceful degradation is the practice of building an application for modern browsers while ensuring it remains functional in older browsers. Graceful degradation is one solution. It is the practice of building a web site or application so it provides a good level of user experience in modern browsers. However, it will degrade gracefully for those using older browsers. Since HTML is continually changing and different browsers support different elements, graceful degradation is the key to making sure that pages are readable and accessible in all browsers. When a
browser encounters tags it doesn't understand or can't display, degradation takes place. Whether this degradation will cause some of your page content to be lost to the browser, or whether the content of your page can still be accessed fully is dependent on whether the degradation is graceful.
For Example
A simple example is the use of 24-bit
alpha-transparent PNGs. Those images can be displayed on modern browsers without problems. IE5.5 and IE6 would show the image, but transparency effects would fail (it can be made to work if necessary). Older browsers that do not support
PNG would show alt text or an empty space.
Using Modernizr to Provide Graceful
Degradation
- Modernizr.load({
- test: Modernizr.geolocation,
- yep: '../js/fullGeolocation.js',
- nope: '../js/geolocationFallback.js'
- });