2

I'm using this condition to detect portrait or landscape of ios devices. But it's not working. What might be the problem?

if (navigator.standalone) {
    var windowHeight = $(window).height();
    var windowWidth = $(window).width();

        if(windowWidth>windowHeight) {
            $('body').css('border','0');
        }else{
            $('body').css('border-top','20px solid #c23c2f');
        }
}

The (windowWidth>windowHeight) seems to be not working, since, it only works on the first load of the page, if the user uses landscape as default, then when tilted to portrait, the condition is not met.

kgam
  • 217
  • 3
  • 4
  • 12

1 Answers1

0

When is this code executed?

You should listen for the orientationchange event, and then execute your code

Reference: https://developer.apple.com/library/ios/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html

The reference adds the event listener like this:

<body onorientationchange="updateOrientation();">

but you can add it with jquery as well, and you can determine the orientation of the screen with window.orientation if you dont want to do the math, or its unreliable

BobbyTables
  • 4,481
  • 1
  • 31
  • 39
  • This executes when you install the website as web app on iPhone. "Add to Home Screen" – kgam Aug 26 '15 at 12:03