Ian Fraser
Ian Fraser

Reputation: 143

Is there anyway of telling if a landscape window is 90 or -90 degress without turning it?

I've been looking around for a solution like:

$(window).on("orientationchange",function(){
    if(window.orientation == 90) {
        console.log('This is 90 degrees');
    } else if(window.orientation == -90) {
        console.log('This is -90 degrees');
    } else {
        console.log('This is portrait');
    }
});

But the problem is I want to detect which way the orientation is without having to turn the screen. I'd like the screen to load and to already know it's either at 90 degrees or set to -90 degrees. Any ideas?

Upvotes: 0

Views: 31

Answers (1)

Nilesh Mistry
Nilesh Mistry

Reputation: 337

Please check following code which will work on page load and orientation change,

  <script>
  function changeOrientation()
  {
    switch(window.orientation) 
    {  
      case -90:
       alert('landscape -90');
        break; 
      case 90:
        alert('landscape 90');
        break; 
      default:
        alert('portrait');
        break; 
    }
  }

  window.addEventListener('orientationchange', changeOrientation);

  // on page load call
  changeOrientation();
  </script> 

Upvotes: 1

Related Questions