New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

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

Mobile Web Design & Development Fundamentals
Illustration by

Detecting orientation changes


From:

Mobile Web Design & Development Fundamentals

with Joe Marini

Video: Detecting orientation changes

On the iPhone, your web pages can also detect changes in the device orientation, and you do that by responding to orientation events. The orientation events look like this. First, there is the onorientationchange, which is the event that's fired on the body element. You can assign a function to that handler to respond to changes in the orientation. You can also look at the orientation property on the window to see what the current orientation is. Now, it should be pointed out that window.orientation is read only.
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 orientation changes

On the iPhone, your web pages can also detect changes in the device orientation, and you do that by responding to orientation events. The orientation events look like this. First, there is the onorientationchange, which is the event that's fired on the body element. You can assign a function to that handler to respond to changes in the orientation. You can also look at the orientation property on the window to see what the current orientation is. Now, it should be pointed out that window.orientation is read only.

You can't programmatically set the orientation and in the onorientationchange event, there is no way to prevent it from happening. The orientation is going to change, regardless of what your code tries to do. So let's take a look at example of handling this event to see how we can respond to device orientation changes in our code. So here I am in Xcode, and I've got my orientation_start example open, and this is just a very simple orientation example. So what I am going to do is have a function here called updateOrientation, and in this little div down here with the ID of output, I am just going to show what the current orientation is.

So I am going to go over my snippets file, and you can see I am scrolled down to the simple orientation example. The first thing I am going to do is just copy the onorientationchange handler and paste that onto the code here for the body. So now I've got the onorientationchange handler. The other thing I am going to do is, in onload, I am going to call updateOrientation as well. That way, that div has some initial value, and we're not just getting a blank white screen. So I am going to save that.

Now I am going to copy over the code that actually handles the updating of the orientation. That's this function right here. So I want to copy this, scroll down right there, and I am going to copy and I am going to paste, and I am going to paste it right inside this function here. So, let's take a quick look at the code. You can see that I've got a variable called displayString and that says Orientation. And then based on what the value of the window's orientation is, whether it's 0 or -90, or 90 or 180 degrees, I'm going to update the displayString in the output div to match the string that I've constructed here.

So as I change the device orientation, it will say things like Orientation Portrait or Orientation Landscape. So let's go ahead and save that, and let's try it out. So I'll go over to the Simulator, and you can see that I've currently got the file loaded. And just to show you how it works, in the Hardware menu, you can do things like Rotate Left and you can do Rotate Right. Now, this is the initial state of the file. So let me just copy over the edited one to the web server, and I'll copy over orientation_start.

Yes, I'll replace it, okay, and go back to the simulator. Now, I am going to reload it, and you can see when I reload it, the window's onload handler gets called, and the current state of the device is in Portrait mode, so it's showing 0 degrees, and we'll move it over here. So now I am going to rotate it to the left and you can see that now the orientation is at landscape, which is 90 degrees counterclockwise, and I'll rotate it right and then right again. And now you can see that the landscape is at -90 degrees. Now, I tried doing upside down, but apparently the iOS Simulator doesn't like the upside-down setting, but there is a 180 setting that your application can detect, just so you know.

But it looks like the Simulator is not doing that. But anyway, using the window.orientation and the onorientationchange event, your web applications and web pages can respond to changes in the way the hardware is oriented.

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

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

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

Are you sure you want to delete this note?

No

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
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.