DCSIMG
Detecting HTML5 Features Using Modernizr - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Detecting HTML5 Features Using Modernizr

Detecting HTML5 Features Using Modernizr

In the past I wrote a post about using feature detection instead of browser Detecting HTML5 Features Using Modernizrdetection when writing web applications/sites. There are frameworks like jQuery that do that for you and all you have to do is to use them. There are other frameworks like Modernizr that are specializing in testing the current browser you use in order to detect whether it supports upcoming features such as HTML5 or CSS3.

What is Modernizr?

Taken from Modernizr site: “Modernizr is a small and simple JavaScript library that helps you take advantage of emerging web technologies (CSS3, HTML 5) while still maintaining a fine level of control over older browsers that may not yet support these new technologies.”

How to use the Modernizr?

Using Modernizr is very easy. First download the library from Modernizr’s site. Then add the javascript file you downloaded to your site and use Moderinzr API. For example, here is how you’ll detect whether HTML5 Canvas is available in the browser:

// In your BLOCKED SCRIPT
if (Modernizr.canvas) {
   var c = document.createElement('canvas');
   var context = c.getContext('2d');    
   // Do your work 
}

Here is an example of detecting search input type:

<input type="search" name="searchBox" id="searchBox">
 
<script>  
if (!Modernizr.inputtypes.search){
  // if no native support, create your own search box
  createSearchBox(document.getElementById('searchBox'));
}
</script>

Here is a check for SVG:

if (Modernizr.svg)
{
    // SVG is supported by your browser
} else 
{
    // SVG is not supported
}

As you can see the API is very easy to use and easy to pick up. For further information about the API you can go to Modernizr’s documentation.

Summary

Feature detection can help you to create a more stable web application/site. Modernizr is a javascript library that can help you to detect upcoming features such as HTML5 or CSS3. It is very easy to use and can be downloaded from here.

Comments

HTML Scripts Tips and Secrets » Blog Archive » Detecting HTML5 Features Using Modernizr – Gil Fink on .Net said:

Pingback from  HTML Scripts Tips and Secrets  &raquo; Blog Archive   &raquo; Detecting HTML5 Features Using Modernizr &#8211; Gil Fink on .Net

# January 9, 2011 10:37 PM

Twitter Trackbacks for Detecting HTML5 Features Using Modernizr - Gil Fink on .Net [microsoft.co.il] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Detecting HTML5 Features Using Modernizr - Gil Fink on .Net         [microsoft.co.il]        on Topsy.com

# January 10, 2011 7:02 PM

Gil Fink on .Net said:

Book Review – HTML5: Up and Running In the last two weeks I read the book “HTML5: Up and Running” that

# January 14, 2011 5:46 PM

Gil Fink's Blog said:

The page is an index of all the HTML5 posts that I wrote: Detecting HTML5 Features Using Modernizr Using

# May 8, 2012 12:12 PM