0

I am using the following code to check the orientation of the iPad. This method is getting called only when i rotate the device or change the orientation of iPad. If a launched the app in landscape mode and when i navigate to this screen the below method is not getting called. How can i detect the orientation of the iPad without rotating the iPad.

$(window).bind('orientationchange', function (event) {
    if (event.orientation == "landscape") {
        set frame for landscape
    } else {
        set frame for portrait
    }
});
Warrior
  • 39,156
  • 44
  • 139
  • 214

3 Answers3

0

This will check the iPad existence and then compare the height with width.

var isiPad = navigator.userAgent.indexOf('iPad') != -1

//or
var ua = navigator.userAgent;
var isiPad = /iPad/i.test(ua)
if (isiPad) {
$(window).on('load orientationchange', function(event) {
    if(window.innerHeight > window.innerWidth){
        console.log("portrait");
    } else {
        console.log("landscape");
    }
});
}
Aakash Dave
  • 866
  • 1
  • 15
  • 30
Matt
  • 1,081
  • 15
  • 27
0

This should work too:

$(window).bind('orientationchange', function(event) {
  alert('new orientation:' + event.orientation);
});
michelem
  • 14,430
  • 5
  • 50
  • 66
0
$(window).on('load orientationchange', function(event) {
    if(event.orientation == "landscape")
    {
        set frame for landscape
    }
    else
    {
        set frame for portrait
    }
});

Add load event as well. This will help if the screen in question is available in DOM when window.load is triggered. Else trigger the event after the screen is injected to DOM dynamically(as suggested by @Tushar).

Keep in mind that orientation change or resize events will not be triggered as page loads initially, if you want to run them initially you will have to include load event as well. Or trigger them later if its dependent on dynamic parts of DOM.

sabithpocker
  • 15,274
  • 1
  • 42
  • 75