Continuing with the Modernizr series, we now start with the implementation.
So how to go about implementing these compatibility checks?
Listing 1 provides us an idea about implementing the user-agent header check (a.k.a. UA sniffing).
Listing 1: Function with user-agent header check
Though UA sniffing has been a standard practice, nevertheless it poses some issues. Considering the above example, we are faced with the inability to ascertain the exact ‘MSIE’ version in this case. Moreover, it also doesn’t provide any function specific checking.
For checking versions, the workaround is to use the navigator object in combination with regular expressions to detect the various browsers and their exact versions.
Specifically, we would query the navigator.appName and navigator.userAgent properties. However, using this approach again won’t solve the feature detection handicap mentioned above.
It would be both faster and more reliable to have the ability of using probing functions which can confirm the availability of objects, methods, attributes and so on (i.e. catering to the 4 points mentioned in the previous section)
We can always create these probing functions which then query for the required stuff, and if not found return null. But the overheads for these custom monitor functions increase if we are having a big web based project, intended to run on multitude browsers.
Modernizr – an introduction
Unlike the UA sniffing examples above, feature detection using this library is more reliable and convenient.
The developer version of the script library can be downloaded from http://modernizr.com/downloads/modernizr-latest.js. This URL always points to the latest version (which is v2.6.2 as of writing).
The above version is an uncompressed script version and contains all the possible checks the library offers. Ideally, this should be used for development and learning purposes.
For use in a production environment, a feature selection tool or a build tool is provided (http://modernizr.com/download/) which can be used for selecting only the required feature detection tests.
On generation, this will create a feature specific, optimized and compressed version of the script, ideal for production environment.
Figure 1: Screen shot showing the stats results after profiling JS code.
Using Modernizr hands-on
After getting the script to your local workspace, you need to include it in your document’s <head> tags.
Listing 2: Including Modernizr script in an HTML document
After including it in your document, you have a variety of functions at your disposal to check for the required browser capability or features.
As we know in HTML5 the <canvas> element provides a bitmap based canvas for rendering custom graphics (chart or game related) and other images. So as an example, let’s use the script to check for the canvas element.
Listing 3: Checking for canvas support
So Listing 3 helps us confirm (easily) that the client browser is actually capable of showing our canvas effects, or if we have to use the fallback alternatives (which we need to have in place).
We can also couple up the element specific getter functions with the probing functions as shown in Listing 4.
Listing 4: Creating a canvas context when canvas support exists in browser
There's many more ways and inbuilt functions to check for the browser specific HTML5 and CSS3 features.
You can explore these further by referring to the Modernizr homepage (check the links in the conclusion / follow-up section).
Modernizer – The shortfalls (what cannot be detected)
Although the Modernizr library tries to make feature speculation more conclusive, it does have its shortfalls. Some of the featured which are yet undetectable are:
· Drag/Drop feature
· Some UI detection (web-forms / date-picker / color-picker UI etc.)
· Audio / Video loop attribute
· Font rendering effects (anti-aliasing, smoothening etc.)
· Keyboard events and event process priority (mouse & keyboard) and so on.
It is hoped that the coming versions would cater to more feature checks, until then the above features can be detected with use of older techniques like UA sniffing.
Links in this conclusion section can be pursued for further details and usage of the same.
Conclusion & follow up
The above introduction should help us evaluation browser capabilities faster and more accurately. In coming issues we will further discuss HTML5’s other new concepts. More information about Modernizr can be had by visiting the following links:
· About HTML5 – http://en.wikipedia.org/wiki/HTML5
· HTML5 & CSS3 latest features in action! A video presentation - http://channel9.msdn.com/Events/Build/2012/3-114
· Modernizer Homepage - http://modernizr.com/
· Modernizer Github - https://github.com/Modernizr/Modernizr
· HTML fallback list – https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills
· Modernizr Undetectable features – https://github.com/Modernizr/Modernizr/wiki/Undetectables