JavaScript Capture IOS7 iPad Screen Orientation Change

Sam Deering

Code snippet to capture ipad device change (IOS7) tested.

 
jQuery(document).ready(function($) {
//capture ipad device change (IOS7) tested.
function doOnOrientationChange()
{
  // alert(window.orientation);
  switch(window.orientation)
  {
    case 0:
    case 90:
      alert('portrait');
      $('html').removeClass('landscape').addClass('portrait');
      break;
    case -90:
      alert('landscape');
      $('html').removeClass('portrait').addClass('landscape');
      break;
    default:
      break;
  }
}
});

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • Dustin

    This is a function which detects the current orientation on page loads, and then adds a class to the document. It in no way detects if there is an orientation change after the document has already loaded.

    • http://jquery4u.com/ jQuery4u

      It works for me, if you know of a better way please share.