Watching:

Detecting client capabilities with script


show more Detecting client capabilities with script provides you with in-depth training on Developer. Taught by Joe Marini as part of the Mobile Web Design & Development Fundamentals show less
please wait ...

Detecting client capabilities with script

In the previous chapter, we looked at techniques and approaches for adapting content to mobile devices. In this chapter, we're going to take a look at techniques for creating pages that are optimized for mobile devices, and we're going to do that starting off by looking at how to detect the capabilities of a mobile client by using JavaScript. Ordinarily, you've probably seen websites that you use user agent detection on the server side to detect capabilities of the device that is viewing the site. Ordinarily, I would say to avoid this because it's inherently backward looking, it's hard to get right, and it's hard to future-proof.

And we'll discuss that a little later when we actually take a look at doing server-side detection, some best practices there. The nice thing about using script to determine a browser's capabilities is that you're detecting features; you're not detecting specific browsers. So instead of looking at the incoming user agent and saying, "Aha! This is browser X and I know the browser X can do this and that," you're actually testing for the feature support on the client side regardless of what the browser is before you try to use that feature.

The general method for doing this is you detect support for something, whether it's HTML5 Canvas or video or support for a geolocation or local storage or some CSS3 property, whatever it is, you detect the support, you see if the browser can actually do it, and then you enable code that relies on that feature if that feature is present. So let's take a look at an example of that. Here is a really simple example. In this example we're just detecting support for geolocation services.

So the first line of code says, if (!!navigator.geolocation). You may not have seen the double exclamation point before. It's commonly used in JavaScript. It basically takes a property that's not normally a Boolean value and forces it to be a Boolean value. So this is just saying if not, not navigator.geolocation, and two nots make a true. So this is saying convert this to a Boolean value and saying, "Hey if navigator.geolocation exists then the device supports geolocation, so we can go ahead and call the API functions." If that expression evaluates to be false, that means that support for geolocation is not there and we would need to fall back to some other kind of implementation, for example explicitly asking the user to supply an address.

Well, let's take a look at a more complex example. In this case, we're detecting support for the autofocus attribute on an input element, and this is part of the HTML5 input specification for forms. There is an attribute called Autofocus, and if it's present then the browser will place the focus on then input type, and we can detect to see if that's there. We can say, "Hey, is the autofocus attribute in the properties listed by the document.create element for an input?" So this code right here will actually make an input element and then write a test to see if that element contains an autofocus property, and again using the double-bang or double-not notation here to force it to be Boolean.

And if this expression evaluates to be true then we know that autofocus is supported on the input element; otherwise we have to fall back to some custom JavaScript code. Up until recently, you'd have to write detection code like this yourself. You'd have to figure out how to detect if a certain feature is supported and then write a script for it. The good news is there's a nice third- party library that does this now, and it's called Modernizr. And if you're following along in the chapter where we set up the development environment, I had to download Modernizr as part of course. Modernizr is a small JavaScript library that detects dozens of features in a lot of different browsers.

It's free, it's open source, and it's available from modernizr.com. To be clear, Modernizr does not add support for missing features. It just detects whether they are there or not. It does this by creating a global JavaScript object named modernizr that contains attributes that indicate whether features are or aren't supported natively in the browser. And if they're not, there are other JavaScript libraries you can use that fill in missing support for some features, but Modernizr just acts as a detector.

Modernizr also adds CSS classes to the HTML element of a document, so that you can do really cool things with your CSS code like if/else statements kind of style logic to display or hide elements, for example, based upon whether or not some features are present. So let's take a look at how to use Modernizr. Let's suppose we had a HTML file that looks like this. Step #1 would be to add the script tag that creates a link to the Modernizr script library. You can see I've done that right here.

Step #2 would be to add the "no-js" class to the HTML tag at the top of the file, and you do that because in the case where JavaScript is disabled and Modernizr won't run you can still use CSS to detect the fact that JavaScript is not turned on. Step #3 would be to find CSS styles to use for a particular Modernizr test results, for example, when a feature is or isn't supported. And then finally, step #4 would be use the Modernizr JavaScript object to see if features are supported in your script code in real time.

Let's take a look at an example in code of how this works. You can see that we're going to detect some JavaScript features, some HTML5 features, and some CSS features. So for JavaScript, we'll do a test to see if geolocation and touch events are supported. For HTML5, we'll see if the device supports SVG and Canvas. And in the CSS3 section, one of these two paragraphs are going to show up based upon whether or not the device supports CSS3-style animations. So let's go back to source code, so I'm going to go to my snippets file, and I'm going to include the link to the Modernizr JavaScript library, and I'll just paste that in here.

The next thing I'm going to do is add the class="no-js" to my html tag, and then I'm going to copy and paste this script code right here that will test to see if certain features are present. I'm going to copy that in here and paste it. So the code that I've just copied does a couple of things. It uses the Modernizr object to see if a particular property is present, like Geolocation or Touch, and based upon the result of whether that evaluates are true or not, it's going to set the innerHTML of the span that I have down here in the document to indicate whether or not that thing is supported.

So we do the test and based upon whether it's true or false, we have supported or not supported. So before we go any further, let's just save that and test that out and see if it works. So I'm going to copy featuredetect_start into my exercise folder for Chapter 5 in the web server, and let's tried out an Android first. I'll write http, and we can see in Android that the JavaScript features, geolocation is supported, Touch Events are supported, SVG in this case is not supported, and Canvas is supported.

So let's do one more quick test on this in the Windows phone. Let me going to see that on the Windows Phone emulator geolocation is there, Touch Events are not there. However, SVG and canvas are supported. Now let's go back to the code and add the CSS3 detection, so back here in the code. Now we're going to see if CSS3 animations are supported, and the way we're going to do that is by using this code here. So let me copy it over and explain what it does. And we'll put that in </head>.

In this case, I'm declaring two CSS classes, one called animtest and one called noanimtest, and I'm setting the display to none. So if you look down here, you'll see that that corresponds to these two paragraphs, so both of these will start out as being hidden. When Modernizr runs, it replaces the--back up here-- it replaces the "no-js" with CSS styles that are named appropriately for different CSS abilities of the browser. So for example, if cssanimations are supported, it will create a class name on the HTML element called cssanimations; or if it's not there, it'll put a "no-" in front of it.

So among the whole bunch of other class names, there will be something that looks like this, or it'll look like this if the device does not support it. And you can use that CSS information to show or hide various features on your page. So what I'm doing here is declaring a class that looks for cssanimations if it's present, and then animtest inside that and displaying it as a block, so that if animations are present this paragraph will show up, and the same thing for no-cssanimations and the noanimtest.

So if they're not supported then this paragraph will show up. So let's go ahead and save, and let's copy it back in. Okay. And now let's refresh. And if we scroll down, you'll see here at the bottom that cssanimations are in fact supported. So the Android browser is supporting cssanimations. Let's do the same thing on Windows Phone, and you can see if we scroll down here to the bottom, cssanimations are not supported.

So that's how you use Modernizr to detect feature support in both JavaScript and CSS in your web pages.

Detecting client capabilities with script
Video duration: 10m 8s 5h 47m Beginner

Viewers:

Detecting client capabilities with script provides you with in-depth training on Developer. Taught by Joe Marini as part of the Mobile Web Design & Development Fundamentals

Subjects:
Developer Web
Software:
HTML
Author:
please wait ...