Zum Inhalt springen

Modernizr

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 26. Mai 2012 um 15:23 Uhr durch imported>LittleBenW (References). Sie kann sich erheblich von der aktuellen Version unterscheiden.

Vorlage:Infobox software 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.[1][2] This lets developers take advantage of new features that browsers support, yet create fallbacks for browsers that lack support. Modernizr 2.0 was a .net Award 2011 winner for Open Source App of the Year, and one of its creators, Paul Irish won a Developer of the Year award.[3] The related subjects Progressive enhancement and Responsive design were #1 and #2 respectively on the .net list of Top Web Design Trends for 2012.[4]

How it works

Modernizr analyzes the browser's user agent property, as well as uses feature detection, to discern what a browser can and cannot do. It uses both (rather than just the user agent) 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.

Modernizr tests for over 40 next-generation features, then creates a JavaScript object (named "Modernizr") that contains the results of these tests as boolean properties. It 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; it detects native availability of features.

Examples

Modernizr JavaScript Example:

<!DOCTYPE html>
 <html class="no-js">
  <head>
    <title>Modernizr - Javascript Example</title>
 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
    <script src="modernizr.js" type="text/javascript"></script>
    <script type="text/javascript">
      $(document).ready(function()
      {
        if (Modernizr.websockets)
        {
          $("#result").html('Your browser has support for Web Sockets');
        }
        else
        {
          $("#result").html('Your browser does not support Web Sockets');
        }
      });
    </script>
  </head>
  <body>
    <p id="result"></p>
   </body>
 </html>

Modernizr CSS example:

 <!DOCTYPE html>
 <html class="no-js">
  <head>
    <title>Modernizr - CSS Example</title>
 
    <style type="text/css" media="screen">
      div.wsno, div.wsyes { display: none }
      .no-websockets div.wsno { display: block }
      .websockets div.wsyes { display: block }
    </style>
 
    <script src="modernizr.js" type="text/javascript"></script>
  </head>
  <body>
 
    <div class="wsno">
      Your browser does not support WebSockets.
    </div>
 
    <div class="wsyes">
      Your browser supports WebSockets.
    </div>
   </body>
 </html>

See also

References

Vorlage:Reflist

  1. dive/URL
  2. W3 HTML5/URL
  3. Daniel/Url
  1. Faruk Ateş: Taking Advantage of HTML5 and CSS3 with Modernizr. 22. Juni 2010;.
  2. Gil Fink: Detecting HTML5 Features Using Modernizr. 10. Januar 2011;.
  3. .net Awards 2011:#7. Open Source App of the Year: Modernizr 2.0, #16. Developer of the Year: Paul Irish
  4. 15 top web design and development trends for 2012. 9. Januar 2012;.