Start learning with our library of video tutorials taught by experts. Get started

Mobile Web Design & Development Fundamentals
Watching:

Detecting client capabilities with script


From:

Mobile Web Design & Development Fundamentals

with Joe Marini

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.
Expand all | Collapse all
  1. 2m 48s
    1. Welcome
      1m 2s
    2. Using the exercise files
      1m 46s
  2. 29m 25s
    1. Understanding the mobile context
      8m 5s
    2. A survey of mobile sites
      11m 44s
    3. Targeting mobile browsers
      4m 31s
    4. Previewing a complete mobile site
      5m 5s
  3. 27m 20s
    1. Designing for one web
      3m 43s
    2. Using well-defined web standards
      3m 45s
    3. Designing mobile-friendly pages
      3m 40s
    4. Being crisp, clean, and succinct
      5m 45s
    5. Minimizing input where possible
      6m 47s
    6. Focusing on the core scenarios
      3m 40s
  4. 1h 13m
    1. Installing the tools
      3m 52s
    2. Setting up a local web server
      9m 13s
    3. Installing device emulators
      17m 5s
    4. Using device emulators
      13m 9s
    5. Downloading Modernizr and Mobile Boilerplate
      6m 22s
    6. Building a first mobile web page
      5m 43s
    7. Developing mobile pages with desktop browsers
      8m 6s
    8. Exploring useful mobile web development resources
      10m 23s
  5. 53m 26s
    1. Reviewing mobile markup languages
      5m 10s
    2. Understanding content adaptation approaches
      10m 32s
    3. Controlling the viewport layout
      12m 50s
    4. Designing forms
      13m 30s
    5. Using CSS media queries
      11m 24s
  6. 1h 11m
    1. Detecting client capabilities with script
      10m 8s
    2. Caching information with local storage
      9m 16s
    3. Determining position with geolocation
      12m 52s
    4. Minimizing HTTP requests with data URLs
      7m 39s
    5. Understanding user agent detection
      9m 8s
    6. Using server-side detection with PHP
      6m 52s
    7. Using server-side detection with ASP.NET
      4m 54s
    8. Using HTML5 Boilerplate for mobile
      11m 6s
  7. 39m 22s
    1. Measuring performance
      7m 41s
    2. Creating full-screen web apps
      6m 30s
    3. Customizing the user interface
      5m 14s
    4. Detecting orientation changes
      3m 58s
    5. Detecting device movement
      5m 21s
    6. Using touch events
      10m 38s
  8. 47m 14s
    1. Taking a look at the finished site
      7m 40s
    2. Examining the header and background image style on the Home page
      10m 10s
    3. Examining the hover effect and layout styles on the Tours page
      6m 42s
    4. Examining mobile forms on the Contact page
      9m 45s
    5. Viewing and testing the mobile site on emulators
      8m 11s
    6. Viewing the site on devices
      4m 46s
  9. 2m 34s
    1. Goodbye
      2m 34s

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
Please wait...
Mobile Web Design & Development Fundamentals
5h 47m Beginner Jul 20, 2011

Viewers: in countries Watching now:

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.

Topics include:
  • Implementing well-defined web standards
  • Working with Modernizr and Mobile Boilerplate
  • Building a first mobile web page
  • Understanding content adaptation strategies
  • Designing forms for mobile
  • Detecting client capabilities with script
  • Using server-side detection with PHP and ASP.NET
  • Working with mobile browser capabilities
  • Viewing and testing the mobile site on device emulators
Subjects:
Developer Web Web Foundations Mobile Web
Software:
HTML
Author:
Joe Marini

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 .

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.

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

Share a link to this course
Please wait... Please wait...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

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.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Mobile Web Design & Development Fundamentals.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

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

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

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:

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:

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

   
submit Lightbox submit clicked