Modernizr
Stable release | 2.7.1 / December 7, 2013 |
---|---|
Written in | JavaScript |
Type | JavaScript library |
License | MIT; it was dual-licensed MIT-BSD from June 14, 2010[1] to September 15, 2012[2] |
Website | modernizr.com |
In web development, Modernizr is a small JavaScript library that detects the availability of native implementations for next-generation Web Technologies. These technologies are new features that stem from the ongoing HTML5 and CSS 3 specifications.
Overview
Many HTML5 and CSS 3 features are already implemented in at least one major browser. Modernizr tells you whether the current browser has implemented a given feature.[3][4][5][6] This lets developers take advantage of new features that browsers support, yet create fallbacks for browsers that lack support. In both 2010 and 2011, Modernizr won the .net Award for Open Source App of the Year, and in 2011 one of its lead developers, Paul Irish, won the Developer of the Year award.[7] The related subjects Progressive enhancement and Responsive design were #1 and #2 respectively on the .net list of Top Web Design Trends for 2012.[8]
How it works
Modernizr uses feature detection, rather than checking the browser's property, to discern what a browser can and cannot do. It considers feature detection more reliable since the same rendering engine may not necessarily support the same things in two different browsers using that engine. In addition, some users change their user agent string to get around websites that block features for browsers with specific user agent settings, despite their browsers having the necessary capabilities.
Modernizr offers tests for over 150 next-generation features, then creates a JavaScript object (named "Modernizr") that contains the results of these tests as boolean properties. It also adds classes to the HTML element based on what features are and are not natively supported.
Tests
To perform feature detection tests, Modernizr often creates an element, sets a specific style instruction on that element and then immediately tries to retrieve that setting. Web browsers that understand the instruction will return something sensible; browsers that don't understand it will return nothing or "undefined". Modernizr uses the result to assess whether that feature is supported by the web browser.
Many tests in the documentation come with a small code sample to illustrate how you could use that specific test in your web development workflow.
Running
Modernizr runs automatically. There is no Modernizr.init() function to call. When it runs, it creates a global object called Modernizr that contains a set of Boolean properties for each feature it can detect. For example, if your browser supports the canvas API, the Modernizr.canvas property will be true. If your browser does not support the canvas API, the Modernizr.canvas property will be false:
if (Modernizr.canvas) { // let's draw some shapes! } else { // no native canvas support available :( }
What Modernizr doesn't do
Modernizr does not add missing functionality to browsers. However, the HTML5 Shiv JavaScript library adds basic support for HTML5 elements in Internet Explorer prior to version 9,[9] and this and other such polyfills are listed on the Modernizr Wiki.[10]
Examples
Modernizr JavaScript example
<!DOCTYPE html> <html class="no-js" lang="en"> <head> <title>Modernizr - JavaScript Example</title> <script src="path/to/modernizr.js"></script> </head> <body> <p id="result"></p> <script> elem = document.getElementById('result'); if ( Modernizr.websockets ) { elem.innerHTML = 'Your browser supports WebSockets.'; } else { elem.innerHTML ='Your browser does not support WebSockets.'; } </script> </body> </html>
Modernizr CSS example
<!DOCTYPE html> <html class="no-js" lang="en"> <head> <title>Modernizr - CSS Example</title> <style> .wsno, .wsyes { display: none; } /* Modernizr will add one of the following classes to the HTML element based on whether or not WebSockets is supported by the user's browser. */ .no-websockets .wsno, .websockets .wsyes { display: block; } </style> <script src="path/to/modernizr.js"></script> </head> <body> <p class="wsno">Your browser does not support WebSockets.</p> <p class="wsyes">Your browser supports WebSockets.</p> </body> </html>
See also
- HTML
- HTML5
- HTML5 Shiv
- HTML5 File API
- HTML5 in mobile devices
- Comparison of layout engines (HTML5 support)
- Comparison of layout engines (CSS support)
- JavaScript
- List of JavaScript libraries
- Polyfill
- Web Workers
- WebSocket
- WebGL
References
- ↑ "Modernizr 1.5: new features, unit tests added". Modernizr. 14 June 2010. Retrieved 30 July 2013.
- ↑ "Remove BSD license and improve readme". GitHub. 15 September 2012. Retrieved 30 July 2013.
- ↑ Faruk Ateş (June 22, 2010). "Taking Advantage of HTML5 and CSS3 with Modernizr".
- ↑ Gil Fink (Jan 10, 2011). "Detecting HTML5 Features Using Modernizr".
- ↑ Daniel Sellergren (Feb 2011). "Using Modernizr to Determine HTML5 CSS3 Support".
- ↑ David Powers. "Using Modernizr to detect HTML5 and CSS3 browser support".
- ↑ .net Awards 2011:#7. Open Source App of the Year: Modernizr 2.0, #16. Developer of the Year: Paul Irish
- ↑ "15 top web design and development trends for 2012". January 9, 2012.
- ↑ "HTML 5 elements in IE". Retrieved 2012-06-14.
- ↑ HTML5 Cross Browser Polyfills
External links
- Official website
- "Drupal integration with Modernizr".
- W3C HTML5
- W3C geolocation
- Web workers
- Web worker basics
- Creating Cross Browser HTML5 Forms Now
- webdesignernote
- HTML Boilerplate in Ajaxian Archives
- Faruk Ateş. "Proudly Announcing Modernizr".
- "Speeding Up With Modernizr".