Modernizr.js

modernizrModernizr.js is a little library that will help "modernize" old browsers.

As an example, let's say you create a new ASP.NET MVC 3 application with the "Use HTML 5 semantic markup" checkbox selected. If you peek into the layout view for the app, you'll find the following markup:

<nav>
    <ul id="menu">
        <li>@Html.ActionLink("Home", "Index", "Home")</li>
        <li>@Html.ActionLink("About", "About", "Home")</li>
    </ul>
</nav>

The nav element represents a section with navigation links, and is a new element in the HTML 5 spec. The idea is that using nav gives more meaning to the page, and allows user agents like screen readers to identify navigation information (contrast nav against using a regular div element as the container  - a div carries relatively no semantic value by itself).

There is a problem, however. Older versions of Internet Explorer (anything before version 9) do not recognize the nav element. IE refuses to apply style rules to unidentified elements, and will also not allow unidentified elements to have children in the DOM. If you have to support older versions of IE this will sound like a dire situation, but it turns out there is an easy solution.

HTML5 Shiv

You can "register" elements in IE using document.createElement. For example…

document.createElement("foo");

... is all you need to do for IE to apply style rules to <foo> elements, and construct them appropriately in the DOM. Years ago Remy Sharp created the html5shiv project, which was the first library to essentially execute createElement for all of the new HTML 5 elements, including nav, article, footer, header, etc. html5shiv then allows IE to recognize all HTML 5 elements. 

Modernizr also "registers" all HTML 5 elements, meaning the HTML 5 MVC web application you build will work just fine on older versions of IE (as long as you leave Modernizer in your layout view). One note: you'll want to keep the script tag for Modernizr inside the <head> element. All the calls to createElement for new elements must happen before IE reaches the <body> tag.

Enabling HTML 5 elements is just one of the features Modernizr provides. In future posts we'll look at Modernizr's ability to detect features, and inject fallback scripts.

Print | posted @ Thursday, October 06, 2011 9:12 AM

Comments on this entry:

Gravatar # re: Modernizr.js
by Jason Jarrett at 10/6/2011 2:42 PM

Nice overview.

I started to write the following question but stopped and looked it up myself - Case anyone else had the question...

Question - you mention the html5shiv project and you say it does what modernizer is trying to accomplish (but it started earlier?). Do you know why you should chose one over the other.

What a quick search found:

stackoverflow.com/...
  
Gravatar # re: Modernizr.js
by Scott at 10/6/2011 10:22 PM

@Jason: Modernizr will do some additional things that we will look at next week.
  
Gravatar # re: Modernizr.js
by magellings at 10/7/2011 11:03 AM

Sweet! I did not know Modernizr did this. Thanks for sharing.
  
Gravatar # re: Modernizr.js
by Daniel at 10/12/2011 4:35 AM

I've looked at Modernizr and its docu but what it does is still not clear to me:
It just detects (missing) features or it detects (missing) features AND adds them to allow the developer to use them in non compliant browsers?
  
Gravatar # re: Modernizr.js
by tatabánya nyelviskola at 10/12/2011 4:08 PM

same questions as Daniell...
  
Gravatar # re: Modernizr.js
by scott at 10/12/2011 6:27 PM

@Daniel, @tatabanya: By default Modernizr is only detecting features - it doesn't try to fill them in unless you write some more code and pull in additional scripts. This is what we will look at next week.
  
Gravatar # re: Modernizr.js
by tatabánya nyelviskola at 10/15/2011 9:46 AM

@scott: thank you
  
Gravatar # re: Modernizr.js
by John Puddifoot at 10/18/2011 2:42 AM

Hi Scott, very useful, thanks - but surely no-one is still using IE5! :p
  
Comments have been closed on this topic.
Scott Allen
Posts - 869
Comments - 4493
Stories - 14