HTML and CSS Reference
In-Depth Information
Detecting new functions
If you don't know already, in JavaScript, everything is an object
(and there are lots of great topics dedicated to this particular
aspect of JavaScript). Since we're in the browser, the global root
variable is the window object, and functions and methods are
properties on this window object. Because of this, we can test for
new functions, methods, and constructors in the same way as
we did when we were looking for property values.
When I want to test if sessionStorage is available natively in the
browser, I can do:
typeof window.sessionStorage !== 'undefined'
However, older versions of Firefox (3.x in particular) will throw a
security exception if cookies are disabled for this particular line
of code (as we touch on in Chapter 6). As I want this polyfill to
support old versions of Firefox, rather than throwing exceptions
all over the place, I'll wrap the test in a try/catch:
var sessionStorageAvailable = (function() {
try {
return typeof window.sessionStorage !== 'undefined';
} catch (e) {
return false;
As we've already seen, each property and method you're aiming
to polyfill will have its own intricacies when testing in the brows-
ers you plan to support. But this is part of web development
which we're all well and truly used to.
NoTE It's also worth
mentioning now that Mark
Pilgrim, author of “that other
HTML5 book,” HTML5: Up and
Running, put together an abso-
lutely amazing and possibly near
definitive list of methods to
detect features in browsers.
Go to
everything.html to have a look.
Detecting everything when
JavaScript isn't your forte
If JavaScript isn't your bag, there's still hope for you yet. The
Modernizr project ( ) , maintained by Faruk
Ateş, Paul Irish, and Alex Sexton, is a small JavaScript library
that gives you a complete programmatic view of what your
browser does and doesn't support.
Don't be confused by the name though; the library won't mod-
ernise your browser, but it will give you the starting point to eas-
ily detect support for over 40 different new aspects of HTML5
and CSS3. If you wanted to improve your JavaScript, or even
Search WWH ::

Custom Search