Video: 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.

This course surveys the core principles and techniques essential to building web sites for mobile devices. Author Joe Marini introduces the mobile context, sheds light on its unique coding requirements, and discusses interface design techniques that enhance existing sites for mobile viewing. The course shows how to approach designing for mobile form factors such as smaller screens and finger-based interaction, along with how to incorporate CSS3 and HTML5 capabilities, such as geolocation, local storage, and media queries.

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 thatare optimized for mobile devices, and we're going to do that starting off by lookingat how to detect the capabilities of a mobile client by using JavaScript.Ordinarily, you've probably seen websites that you use user agent detection onthe 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 doingserver-side detection, some best practices there.The nice thing about using script to determine a browser's capabilities is thatyou'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 ofwhat the browser is before you try to use that feature.

The general method for doing this is you detect support for something, whetherit's HTML5 Canvas or video or support for a geolocation or local storage orsome CSS3 property,whatever it is, you detect the support, you see if the browser can actuallydo 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 supportfor 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 itto 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 ifnavigator.geolocation exists then the device supports geolocation, so we can goahead and call the API functions."If that expression evaluates to be false, that means that support forgeolocation is not there and we would need to fall back to some other kind ofimplementation, 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 inputelement, 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 willplace 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 thedocument.create element for an input?"So this code right here will actually make an input element and then write atest to see if that element contains an autofocus property, and again using thedouble-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 issupported on the input element; otherwise we have to fall back to somecustom 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 thenwrite a script for it.The good news is there's a nice third- party library that does this now, and it'scalled Modernizr. And if you're following along in the chapter where we set upthe development environment, I had to download Modernizr as part of course.Modernizr is a small JavaScript library that detects dozens of features in a lotof 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 thatcontains attributes that indicate whether features are or aren't supportednatively in the browser. And if they're not, there are other JavaScriptlibraries you can use that fill in missing support for some features, butModernizr just acts as a detector.

Modernizr also adds CSS classes to the HTML element of a document, so that youcan do really cool things with your CSS code like if/else statements kind ofstyle logic to display or hide elements, for example, based upon whether or notsome 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 theModernizr 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 thefile, and you do that because in the case where JavaScript is disabled andModernizr 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 iffeatures 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 HTML5features, and some CSS features.So for JavaScript, we'll do a test to see if geolocation and touch events aresupported. For HTML5, we'll see if the device supports SVG and Canvas. And in theCSS3 section, one of these two paragraphs are going to show up based upon whetheror 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, andI'm going to include the link to the Modernizr JavaScript library, and I'lljust paste that in here.

The next thing I'm going to do is add the class="no-js" to my html tag, and thenI'm going to copy and paste this script code right here that will test to see ifcertain 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, likeGeolocation or Touch, and based upon the result of whether that evaluates aretrue or not, it's going to set the innerHTML of the span that I have down herein 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 havesupported 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 5in 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 notsupported, 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 goingto 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 .

In this case, I'm declaring two CSS classes, one called animtest and one callednoanimtest, and I'm setting the display to none.So if you look down here, you'll see that that corresponds to these twoparagraphs, 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 fordifferent CSS abilities of the browser.So for example, if cssanimations are supported, it will create a class name onthe 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 thatlooks like this, or it'll look like this if the device does notsupport it. And you can use that CSS information to show or hide variousfeatures on your page.So what I'm doing here is declaring a class that looks for cssanimations if it'spresent, and then animtest inside that and displaying it as a block, so that ifanimations are present this paragraph will show up, and the same thing forno-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 thatcssanimations 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 hereto the bottom, cssanimations are not supported.

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

There are currently no FAQs about Mobile Web Design & Development Fundamentals.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Already a member ?

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships.
Learn more

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

new course releases

newsletter

general communications

special notices

Here’s our privacy policy with more details about how we handle your information.