Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 3: Forms > Backwards compatibility with legacy browsers

Backwards compatibility with legacy browsers

The big question is: What can we do for legacy browsers? The answer is that you don’t retire your pre-existing JavaScript validation or fancy DHTML datepickers just yet, but you leave them as a fallback after doing some feature detection.

As we’ve seen before, browsers will fall back to using input type=text whenever they encounter a type that they don’t support. So, a legacy browser, faced with input type=email, will simply change it to an input type=text. This change also happens in the DOM and, by checking the type of the input, we can programmatically determine if the browser supports the new fancy elements, and act accordingly if not.

For instance, to detect whether <input type=email> is supported, you can make a new <input type=email> with JavaScript, but don’t add it to the page. Then, interrogate your new element to find out what its type attribute is. If it’s reported back as “email,” then the browser supports the new feature—so let it do its work and don’t bring in any JavaScript validation. If it’s reported back as “text,” it’s fallen back to the default, indicating that it’s not supported. So your code should load an alternative validation library, ideally through a lazy load technique.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint