HTML5 Day 3: Detecting HTML5 Support via JavaScript

16. November 2010

On Day 1, I covered exactly what HTML5 is and what’s necessary to convince older web browsers to render/style the new tags properly. Once you told the browser how to style the element, it still wont show special UI for the user. The good news is that you can “patch” the browser using JavaScript to enable such functionality. In this post we’ll discuss how to detect if a certain feature is supported by the browser or not.

Why is the UserAgent unreliable for detection?

Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64;
 Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729;
 .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E;
 InfoPath.3; MS-RTC LM 8; Zune 4.7) 

You could always check the UserAgent of the request on the web server and return the necessary javascript accordingly, however, that would require a tremendous amount of work. You would need to figure out what HTML5 features are supported by each version of each web browser that a user might use. That would be a tremendous nightmare.

So the only reliable method is to use a little JavaScript code to do the detection on the client-side. This way you can check if each feature is supported before executing your “patch” code to add support.

Individual Attribute Detection

Lets take the following as an example:

<input type="text" placeholder="enter value" />

One of the new features in HTML5 is the “placeholder” attribute on the <input> tag. This attribute allows you to show some specified text (as a watermark) in the input box until the user enters a value.

Let’s imagine we want to add our own “placeholder” functionality in older web browsers that do not natively support this feature using a JavaScript “patch”. Before we “patch” the browser, we must first detect whether this feature is already supported. To do this, we can use the following JavaScript code:

function IsAttributeSupported(tagName, attrName) {
    var val = false;
    // Create element
    var input = document.createElement(tagName);
    // Check if attribute (attrName)
    // attribute exists
    if (attrName in input) {
        val = true;
    }
    // Delete "input" variable to
    // clear up its resources
    delete input;
    // Return detected value
    return val;
}

if (!IsAttributeSupported("input", "placeholder")) {
    // Do something special here
    alert("placeholder attribute is not supported");
}

You can use the above code with any attributes you want to detect on any element, just change the “input” and “placeholder” within the method call accordingly.

Input Type Detection

There are a number of new input types added in HTML5. The good news is that older browsers will render any unknown types as type=”text”. A couple of the new types are:

<input type="date" />
<input type="email" />

Let’s imagine we want to add you own UI to allow the user to select the date from a date picker control and the email from a list of contacts in older browsers that do not support these features natively. First we must detect if the browser supports these input types. To do this, we can use the following JavaScript code:

funciton IsInputTypeSupported(typeName) {
    // Create element
    var input = document.createElement("input");
    // attempt to set the specified type
    input.setAttribute("type", typeName);
    // If the "type" property equals "text"
    // then that input type is not supported
    // by the browser
    var val = (input.type !== "text");
    // Delete "input" variable to
    // clear up its resources
    delete input;
    // Return the detected value
    return val;
}

if (!IsInputTypeSupported("date")) {
    // Do something special
    alert("date input is not supported");
}

if (!IsInputTypeSupported("email")) {
    // Do something special
    alert("email input is not supported");
}

Now you can execute your own “patch” in older browsers to add custom UI to these input fields.

While there are some exceptions in certain browsers, the above code will work for basic feature detection. One such exception is that the Chrome web browser supports the “date” input, but it doesn’t show any special UI for the user. This is an example where you may want to still show you one UI, but the above detection script will report that “date” inputs are supported in Chrome.

Modernizr Detection Script

You could detect HTML5 features yourself using the previous JavaScript code, but that is probably like using your own JavaScript code to perform animations instead of using jQuery. The Modernizr script does for feature detection as jQuery has done for the HTML DOM itself.

Here’s an example of using Modernizr to detect if the “date” input type is supported:

if (!Modernizr.inputtypes.date) {
    // Do something special
    alert("date input is not supported");
}

You can view the full Modernizr documentation for its full usage.

Additional Resources

Conclusion

Just like most things with JavaScript, it’s easy to perform basic feature detection. Although, you can get a lot of benefit of using a full framework to assist you in the process. Just as I use jQuery over custom HTML DOM code in most cases, I am liking the HTML5 detection support that Modernizr adds.

In tomorrows post I’ll move on to adding some “patch” code to add some support for new features in old browsers now that feature detection is out of the way.

HTML, HTML, JavaScript, JavaScript , , ,

Comments

11/16/2010 9:20:54 AM #
HTML5 Day 1: New tags work in older browser? Awesome!

HTML5 Day 1: New tags work in older browser? Awesome!
12/3/2010 6:27:37 PM #
The new Zune browser is surprisingly good, but not as good as the iPod's. It works well, but isn't as fast as Safari, and has a clunkier interface. If you occasionally plan on using the web browser that's not an issue, but if you're planning to browse the web alot from your PMP then the iPod's larger screen and better browser may be important.
12/3/2010 6:48:18 PM #
I respect your work on this blog and the high-quality posts you make. These type of posts are usually what keeps me personally going through the day time.
12/3/2010 7:30:49 PM #
WowYou evaluate tons of righteous points.
I enjoy your thinkingand you haved got another daily reader.

take it easy
12/8/2010 9:45:50 AM #
Nice article,very Best.
12/9/2010 1:09:26 AM #
There is noticeably a lot to identify about this.  I feel you made various good points in features also. Wow, that's a good read! Thank you!
12/10/2010 10:33:30 AM #
Hi, first my english is not good, so I look for easy english article about this, or som body can explain to me more.
12/12/2010 4:02:42 PM #
Excellent article as usual, thanks!
12/12/2010 6:31:34 PM #
I agree with the comments amazing article btw
12/12/2010 11:28:09 PM #
I personally have embraced the new technologies and the CMS platforms, I think the new tools only make the web designs better. I am glad that new technologies are coming out in web design that make things easier, improved, and better looking for design.
12/13/2010 12:03:27 AM #
Sweet Jesus. This is something I have been searching for. This makes me think about life.
12/13/2010 12:43:04 AM #
Hi! Is that All right if I ask anything kinda off topic? I am attempting to check out this page on my  iphone however it will not display appropriately, will you have any ideas about this problems? Will i need to have an update for my program or something like that? thanks! x Smile
12/13/2010 3:51:15 AM #
You certainly deserve a round of applause for your post and more specifically, your blog in general. Very high quality material.
12/13/2010 5:01:05 AM #
what is the javascript error which I get always I tried to replied
12/13/2010 5:08:13 AM #
Good luck everybody! - I will come back again. Are you on facebook or twitter? Will like to follow you.  

Regards!


12/13/2010 6:29:54 AM #
lol a couple of the reviews bloggers write are silly and unrelated, there are times i wonder whether they at all read the post before writing or whether they just look at the subject of the blog post and compose the very first thought that drifts to their minds. But it is great to see a intelligent commentary every now and then in contrast to the exact same, traditional blog bull which I too many times notice on the blogs. Regards PokemonCraft Admin.
12/13/2010 6:55:36 AM #
This is cool stuff, its nice to be in the know.
12/13/2010 8:02:48 AM #
A Informative post which was really helpful to me. Thank you.
12/13/2010 8:33:59 AM #
A Informative post which was really helpful to me. Thank you.
12/13/2010 9:07:07 AM #
Hey there!  I just want to say that I found your website enough interesting to me.  Usefull thoughts and all is good arranged. Thank you for your work. I will visit your site more ofter from now and I bookmarked it. You can also visit my <a href="www.24hrcrimescenecleaners.com">crime scene cleanup</a> site if you have a time.
12/13/2010 9:47:12 AM #
A Informative post which was really helpful to me. Thank you.
12/13/2010 10:01:38 AM #
You made some good points there. I did a search on the topic and found most people will agree with your blog.
12/13/2010 12:39:31 PM #
Geat post, thansk! I'am sure will be back here soon!
12/13/2010 1:12:26 PM #
Can you email me with a few tips about how you made this blog site look this awesome, I would be appreciative.
12/13/2010 4:27:06 PM #
The info here is exactly what i was trying to find. It would be fantastic to see your write more on this topic...will check back to see
12/13/2010 6:06:07 PM #
How did you make a blog look this good! Email me if you can and share your wisdom. I'd be thankful!
12/13/2010 9:50:26 PM #
Hey - I really enjoyed the post and would like to say it was an outstanding read.
12/13/2010 10:27:49 PM #
Do you like running Blogengine?  If I might --perhaps you should consider adding a few videos.  I dont mean to disrespect what you've done here; its very good, for sure.  But, I think would respond to it more positively if they could be something animated to your thread.  Keep it up, but put a little more into it next time.
12/13/2010 10:29:48 PM #
This is my brand new i visit here. I found lots of entertaining stuff in the blog, especially its debate. From the tons of comments against your articles, I guess I am not alone having all the amusement here! Keep up the wonderful work.
12/13/2010 10:44:27 PM #
Will you be talking more about this particular article.
12/13/2010 11:59:23 PM #
Keep focusing on your blog. I love how we can all express our feelings. This is an extremely nice blog here Smile
12/14/2010 12:05:48 AM #
Been looking for this article for long time ago and finally found here. thanks for sharing this post. appreciate!
12/14/2010 2:11:16 AM #
I comply with the commenter above rocking article by the way