Modernizr: Difference between revisions
No edit summary |
refspam |
||
Line 28: | Line 28: | ||
==Overview== |
==Overview== |
||
Many [[HTML5]] and [[CSS 3]] features are already implemented in at least one major [[web browser|browser]]. |
Many [[HTML5]] and [[CSS 3]] features are already implemented in at least one major [[web browser|browser]].{{cn}} Modernizr determines whether the user's browser has implemented a given feature.<ref>{{cite web | url = http://www.alistapart.com/articles/taking-advantage-of-html5-and-css3-with-modernizr | title = Taking Advantage of HTML5 and CSS3 with Modernizr | author = Faruk Ateş | date = June 22, 2010}}</ref><ref>{{cite web | url = http://www.codeproject.com/Articles/144372/Detecting-HTML5-Features-Using-Modernizr | title = Detecting HTML5 Features Using Modernizr|author = Gil Fink | date = Jan 10, 2011}}</ref><ref>{{cite web|url=http://danielsellergren.com/2011/02/using-modernizr-to-determine-html5css3-support/ | title = Using Modernizr to Determine HTML5 CSS3 Support|author=Daniel Sellergren|date=Feb 2011|url-status=dead|archiveurl = https://web.archive.org/web/20130822001705/http://danielsellergren.com/2011/02/using-modernizr-to-determine-html5css3-support/ | archivedate = 2013-08-22}}</ref><ref>{{cite web|url = http://www.adobe.com/devnet/dreamweaver/articles/using-modernizr.html | title = Using Modernizr to detect HTML5 and CSS3 browser support|author=David Powers}}</ref> 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.<ref>[http://www.netmagazine.com/features/net-awards-2011-winners .net Awards 2011:#7. Open Source App of the Year: Modernizr 2.0, #16. Developer of the Year: Paul Irish]</ref> |
||
==Function== |
==Function== |
Revision as of 19:02, 17 May 2022
Original author(s) | Faruk Ateş |
---|---|
Developer(s) | Faruk Ateş, Paul Irish, Alex Sexton, Ryan Seddon, Patrick Kettner, Stu Cox, Richard Herrera, and contributors |
Initial release | July 1, 2009[1] |
Stable release | 3.11.3
/ September 28, 2020[2] |
Repository | |
Written in | JavaScript |
Type | JavaScript library |
License | MIT; it was dual-licensed MIT-BSD from June 14, 2010[3] to September 15, 2012[4] |
Website | modernizr |
Modernizr is a JavaScript library that detects the features available in a user's browser. This lets web pages avoid unsupported features by informing the user their browser isn't supported or loading a polyfill. Modernizr aims to provide feature detection in a consistent and easy to use manner that discourages the use of failure-prone browser sniffing.[5]
Overview
Many HTML5 and CSS 3 features are already implemented in at least one major browser.[citation needed] Modernizr determines whether the user's browser has implemented a given feature.[6][7][8][9] 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.[10]
Function
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 more than 250 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.
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.[citation needed]
Many tests in the documentation come with a small code sample to illustrate how a specific test can be used in web development workflow.
Running
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 a browser supports the canvas API, the Modernizr.canvas property will be true. If the 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 :(
}
Limitations
The library is simply a feature-detection method and as such, does not add missing functionality to older browsers.[11]
Examples
Modernizr JavaScript example
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<title>Modernizr - JavaScript Example</title>
</head>
<body>
<p id="result">Modernizr won't run if JavaScript is not enabled.</p>
</body>
<script src="path/to/modernizr.js"></script>
<script>
elem = document.getElementById('result');
if (Modernizr.websockets) {
elem.innerHTML = 'Your browser supports WebSockets.';
} else {
elem.innerHTML ='Your browser does not support WebSockets.' ;
}
</script>
</html>
CSS example
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<title>Modernizr - CSS Example</title>
<style>
.wsno,
.wsyes,
.js .no-js { 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>
</head>
<body>
<p class="wsno">Your browser does not support WebSockets.</p>
<p class="wsyes">Your browser supports WebSockets.</p>
<p class="no-js">Modernizr won’t run if javascript is not enabled.</p>
</body>
<script src="path/to/modernizr.js"></script>
</html>
See also
References
- ^ Faruk Ateş (1 July 2009). "Proudly Announcing Modernizr".
- ^ "Releases · Modernizr/Modernizr". Retrieved 28 September 2020.
- ^ "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.
- ^ "What is Modernizr". Retrieved 25 December 2019.
- ^ 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". Archived from the original on 2013-08-22.
- ^ 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
- ^ "HTML 5 elements in IE". Retrieved 2012-06-14.