Determining phone orientation in jquery

I have a code that I wrote that warns the user about mobile phones that change the landscape to basically go to portrait. This works fine, but if the user starts in the landscape, he obviously does not show, because there is no change in orientation. Is there a way to get an exit orientation?

$(window).on("orientationchange",function(){
        if( (window.orientation == 90 && $( window ).width() < 780) || (window.orientation == -90 && $( window ).width() < 780) ) // Landscape
        {
          $('body').append('<div class="phoneLandscape"><h4>Sorry, this site is best viewed in Portrait Mode</h4></div>');
        }
        else // Portrait
        {
          $('.phoneLandscape').remove();
        }
      });
+4
source share
1 answer

jQuery mobile has a method $(window).on("orientationchange"), but if you want to determine the orientation at any given time, try this

if(window.innerHeight > window.innerWidth){
    //portrait
}
if(window.innerWidth > window.innerHeight){
    //landscape
}
+4
source

All Articles