5

Orientationchange event doesn't fire on iOS devices. On desktops and Android devices all works perfectly but OS devices doesn't support this event. How I can determine screen orientation changes on iOS devices?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sergei Kobets
  • 145
  • 1
  • 2
  • 8
  • $(window).on('resize') doesnt trigger on IOS safari too... I dont know why... – Sergei Kobets Dec 24 '16 at 23:50
  • This old question but you can ask why window.orientation returns nothing even `typeof window.orientation` in alert() doesnt shows any info ? Becouse for windows users alert is only way for some kind of debugging.... – Nikola Lukic Aug 25 '21 at 17:38

2 Answers2

5

This is really old, but it recently happened to me.

I made it work with:

window.addEventListener('resize', functionName)

function functionName() {

  if(window.innerWidth > window.innerHeight) {
  
   //then do stuff
  }
  
  if(window.innerWidth < window.innerHeight) {

  //then do other stuff
  }
}
Mailer
  • 127
  • 1
  • 7
  • On my Android tablet, it works well. But on my iPad, this script keeps firing, even when the device is stationary. See https://stackoverflow.com/questions/8898412/iphone-ipad-triggering-unexpected-resize-events. – Frank Conijn - Support Ukraine May 12 '22 at 11:35
0

I got an answer in this post https://stackoverflow.com/a/9824480/1606668

But If you still don't get orientation values then try this:

   function onOriChange() {
        if(!window.orientation){
            if(window.innerWidth > window.innerHeight){
                alert('landscape');
            }else{
                alert('portrait');
            }
        }
    }

    window.addEventListener('orientationchange', onOriChange);

    // Initial execution if needed
    onOriChange();
Community
  • 1
  • 1
Krunal
  • 725
  • 7
  • 15