245

I am building a website specifically for mobile devices. There is one page in particular which is best viewed in landscape mode.

Is there a way to detect if the user visiting that page is viewing it in Portrait mode and if so, display a message informing the user that the page is best viewed in landscape mode? If the user is already viewing it in landscape mode then no message will appear.

So basically, I want the site to detect the viewport orientation, if orientation is Portrait, then display an alert message advising the user that this page is best viewed in Landscape mode.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Dan
  • 10,171
  • 7
  • 38
  • 31

32 Answers32

333
if(window.innerHeight > window.innerWidth){
    alert("Please use Landscape!");
}

jQuery Mobile has an event that handles the change of this property... if you want to warn if someone rotates later - orientationchange

Also, after some googling, check out window.orientation (which is I believe measured in degrees...)

EDIT: On mobile devices, if you open a keyboard then the above may fail, so can use screen.availHeight and screen.availWidth, which gives proper height and width even after the keyboard is opened.

if(screen.availHeight > screen.availWidth){
    alert("Please use Landscape!");
}
Aniruddha Shevle
  • 4,602
  • 4
  • 22
  • 36
tobyodavies
  • 27,347
  • 5
  • 42
  • 57
  • Thanks for your response. Unfortunately, it doesn't solve the problem of detecting the orientation of the device and displaying an alert message if the device is in Portrait mode. – Dan Feb 07 '11 at 03:16
  • `if(window.height > window.width) alert("please view in landscape");` doesn't work? – tobyodavies Feb 07 '11 at 03:17
  • 6
    No, unfortunately not. However, the following script did work: if(window.innerHeight > window.innerWidth){ alert("Please view in landscape"); } – Dan Feb 07 '11 at 03:53
  • I'm having a little trouble getting this working myself. Which element do I need to call this function on? If somebody could post a working example it would be greatly appreciated. – suryanaga May 04 '13 at 18:13
  • This script won't work if there's a metatag %meta{content: "app-id=, app-argument=#{request.url}", name: "apple-itunes-app"}/ on iPhone4 because even in portrait mode x is bigger than y – Mïchael Makaröv Aug 07 '13 at 19:27
  • this is incorrect. it is relative to how the page loaded if you have fixed or absolute positioned divs. – FlavorScape Aug 25 '13 at 23:07
  • 9
    I was overcomplexifying the problem. This is great. So simple. –  Dec 10 '13 at 14:03
  • 88
    On many devices, when the keyboard is shown, the availableWidth will be greater than the height, incorrectly giving landscape orientation. – Pierre Apr 14 '14 at 13:05
  • to make it work `window.orientation` I need to include mobile jquery library in to page? – ARUN Apr 30 '14 at 10:37
  • 1
    It doesn't work because if the keyboard is displayed, window.innerHeight < window.innerWidth is true but you are in portrait mode. – Stefdelec May 30 '17 at 12:59
  • A great quick solution for simple pages. Note that window.orientation doesn't work on all platforms - if you're using a desktop Chrome browsers, for example, that device has the orientation often undefined. – Eric Sep 05 '17 at 06:37
  • @Eric The question was regarding mobile devices. Desktop Chrome is irrelevant. – jarrodwhitley Feb 14 '19 at 19:21
  • 1
    This doesn't work if combined with the `orientationchange` event. It will show the old orientation instead of the new orientation. [This answer](https://stackoverflow.com/a/11734939/4284627) works well combined with the `orientationchange` event. – Donald Duck Jul 17 '19 at 17:26
  • Maybe `window.innerWidth > window.innerHeight ? "LANDSCAPE" : "PORTRAIT"` – Rajib Chy Oct 04 '19 at 07:19
  • 5
    window.orientation deprecated https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation – kluverua Feb 13 '20 at 09:45
  • This actually doesn't work when bringing up the keyboard on some OSes. The keyboard popup will make this information flip even though they are still in the same orientation. Going by screen size doesn't work. I highly recommend everyone uses: https://developer.mozilla.org/en-US/docs/Web/API/Screen/orientation AKA: screen.orientation.type – user2455808 Mar 08 '23 at 13:22
236

You can also use window.matchMedia, which I use and prefer as it closely resembles CSS syntax:

if (window.matchMedia("(orientation: portrait)").matches) {
   // you're in PORTRAIT mode
}

if (window.matchMedia("(orientation: landscape)").matches) {
   // you're in LANDSCAPE mode
}

Tested on iPad 2.

posit labs
  • 8,951
  • 4
  • 36
  • 66
crmpicco
  • 16,605
  • 26
  • 134
  • 210
  • 3
    Support for that feature is kinda weak though: http://caniuse.com/#feat=matchmedia – Ashitaka Sep 06 '13 at 11:57
  • 2
    In Firefox for Android works properly only after the DOM ready event for me. – Inversion Feb 16 '15 at 18:10
  • 25
    Support for this is much stronger now. This seems to be a much more robust answer now then the accepted one. – JonK Jun 28 '16 at 21:56
  • 3
    Beware of this with this (link) solution https://stackoverflow.com/questions/8883163/css-media-query-soft-keyboard-breaks-css-orientation-rules-alternative-solut – Diego Zacarias Jun 02 '17 at 08:21
  • 3
    This is the correct answer now. This feature has full support for all relevant browsers. – jarrodwhitley Feb 14 '19 at 19:39
  • 1
    This falls prey to the issue where the android keyboard causes the width to exceed the height, the browser will report landscape. - mentioned by @dzv3 – cdosborn Feb 21 '19 at 20:58
  • This doesn't work if combined with the `orientationchange` event. It will show the old orientation instead of the new orientation. [This answer](https://stackoverflow.com/a/11734939/4284627) works well combined with the `orientationchange` event. – Donald Duck Jul 17 '19 at 17:27
  • 1
    I've seen this fail on Android webview when checked too soon. It sometimes takes a bit for the height to be registered. If document.documentElement.clientHeight = 0 then this check will always say landscape. Same for the width/height comparison check, of course. – user984003 Mar 08 '22 at 20:09
  • this doesn't work in Android with keyboard visible. To get rid of it without breaking iOS: `window.matchMedia("(orientation: portrait)").matches || (window.outerWidth != screen.availHeight && screen.availHeight > screen.availWidth)` – Litzer Oct 07 '22 at 12:06
112

David Walsh has a better and to the point approach.

// Listen for orientation changes
window.addEventListener("orientationchange", function() {
  // Announce the new orientation number
  alert(window.orientation);
}, false);

During these changes, the window.orientation property may change. A value of 0 means portrait view, -90 means a the device is landscape rotated to the right, and 90 means the device is landscape rotated to the left.

http://davidwalsh.name/orientation-change

Tisho
  • 8,320
  • 6
  • 44
  • 52
Jatin
  • 1,153
  • 1
  • 7
  • 2
  • 2
    On the Nexus 10, and I don't know about other Android 10" tablets, the values are backwards. I.e. 0 is returned when the device is in landscape, not portrait. tobyodavies's answer above seems to work well though across all devices I've tested – Nathan Jun 20 '13 at 11:21
  • 9
    Orientation 0 is given to be the "natural" orientation of the device, and thus is vendor-dependent. It could be portrait or landscape... – Pierre Apr 14 '14 at 12:52
  • on ipad in iframe did not alert anything – Dariux Jun 04 '14 at 06:30
  • 3
    this is not good solution for detection orientation mod of device. Because they return value depend of natural device orientation. Example on Samsung 7' tablet portrait view return 90 but on nexus 4 for portrait they return 0. – Dexter_ns88 Jan 03 '15 at 22:22
  • Not all devices will report the same values: https://notes.matthewgifford.com/a-misconception-about-window-orientation-7231235a94c2#.llqebngde – Neon Warge May 13 '16 at 03:46
  • 3
    On this link: https://notes.matthewgifford.com/a-misconception-about-window-orientation-7231235a94c2#.llqebngde The author showcase different devices from different manufacturers have different interpretation of what is landscape and portrait. Hence different reported values, which you cannot really rely on. – Neon Warge May 13 '16 at 03:47
  • 1
    orientation is unsupported in iOS – fletchsod Jun 29 '21 at 18:20
  • 2
    orientationchange is deprecated – amarinediary Oct 20 '22 at 19:26
41

You can use CSS3 :

@media screen and (orientation:landscape)
{
   body
   {
      background: red;
   }
}
Thomas Decaux
  • 21,738
  • 2
  • 113
  • 124
  • Best answer to me, because you don't need a listener for changed orientation. Although I combine it with modernizr to detect if it is a touch-device, since it makes no sense on desktop or laptop screens. Still not ok for such screens WITH touch capabilities... – Esger Sep 05 '13 at 20:15
  • 4
    Not related to javascript. OP needs javascript solution. – easwee Feb 12 '14 at 09:31
  • 14
    No, he wants to display a message, you can use display:none then display:block to display something. – Thomas Decaux Feb 12 '14 at 17:45
  • 2
    What is the support like for this CSS check? – ChrisF Dec 03 '14 at 00:38
  • 1
    I dont know, but I didnt find a mobile that doesnot support it. Also it seems broken on old Android, when keyboard shows up sometimes the media query is trigged. – Thomas Decaux Dec 03 '14 at 07:56
  • Unfortunately although this is pretty awesome, as Thomas said I found that on iPhone and Android when the keyboard comes up the media query is triggered, if anyone has an answer I raised the issue [here](http://stackoverflow.com/questions/33357414/css-media-query-to-detect-landscape-only-on-mobile-iphone-android) – svnm Oct 27 '15 at 00:08
  • This is great, not giving me any issues when activating the keyboard. – brewpixels Mar 20 '18 at 09:59
23

There are a few ways to do it, for example:

  • Check window.orientation value
  • Compare innerHeight vs. innerWidth

You can adapt one of the methods below.


Check if device is in portrait mode

function isPortrait() {
    return window.innerHeight > window.innerWidth;
}

Check if device is in landscape mode

function isLandscape() {
    return (window.orientation === 90 || window.orientation === -90);
}

Example usage

if (isPortrait()) {
    alert("This page is best viewed in landscape mode");
}

How do I detect the orientation change?

$(document).ready(function() {
    $(window).on('orientationchange', function(event) {
        console.log(orientation);
    });
});
martynas
  • 12,120
  • 3
  • 55
  • 60
12

I think the more stable solution is to use screen instead of window, because it could be both - landscape or portrait if you will resize your browser window on desktop computer.

if (screen.height > screen.width){
    alert("Please use Landscape!");
}
artnikpro
  • 5,487
  • 4
  • 38
  • 40
  • This works in IE 10, Firefox 23 and Chrome 29 (all desktop browsers). – Jakob Jenkov Sep 28 '13 at 10:07
  • 1
    All major browsers support it. IE 7 as well – artnikpro Sep 29 '13 at 09:04
  • not standard, though: https://developer.mozilla.org/en-US/docs/Web/API/Window.screen#Specification – fjsj Nov 07 '13 at 17:25
  • @Duncan No. It works on mobile. It is a pretty old method and it's supported on old safari and android browsers. It may be not very accurate with retina but for detecting viewport orientation it should work fine – artnikpro Oct 20 '16 at 11:50
  • 2
    @artnikpro I tested it yesterday and it appears that Safari returns the physical screen dimensions neglecting the orientation. So screen.width is always the physical width of the screen. – Duncan Hoggan Oct 20 '16 at 12:56
10

In order to apply all of these great comments to my daily coding, for continuity between all my applications, I have decided to use the following in both my jquery and jquery mobile code.

window.onresize = function (event) {
  applyOrientation();
}

function applyOrientation() {
  if (window.innerHeight > window.innerWidth) {
    alert("You are now in portrait");
  } else {
    alert("You are now in landscape");
  }
}
Andrew
  • 4,953
  • 15
  • 40
  • 58
dLindley
  • 101
  • 1
  • 2
10

CCS only

@media (max-width: 1024px) and (orientation: portrait){ /* tablet and smaller */
  body:after{
    position: absolute;
    z-index: 9999;
    width: 100%;
    top: 0;
    bottom: 0;
    content: "";
    background: #212121 url(https://i.stack.imgur.com/sValK.png) 0 0 no-repeat; /* replace with an image that tells the visitor to rotate the device to landscape mode */
    background-size: 100% auto;
    opacity: 0.95;
  }
}

In some cases you may want to add a small piece of code to reload to page after the visitor rotated the device, so that the CSS is rendered properly:

window.onorientationchange = function() { 
    var orientation = window.orientation; 
        switch(orientation) { 
            case 0:
            case 90:
            case -90: window.location.reload(); 
            break; } 
};
cptstarling
  • 769
  • 6
  • 11
8

Don't try fixed window.orientation queries (0, 90 etc doesn't mean portrait, landscape etc):

http://www.matthewgifford.com/blog/2011/12/22/a-misconception-about-window-orientation/

Even on iOS7 depending how you come into the browser 0 isn't always portrait

kev666n
  • 81
  • 1
  • 1
7

I combined two solutions and it works fine for me.

window.addEventListener("orientationchange", function() {                   
    if (window.matchMedia("(orientation: portrait)").matches) {
       alert("PORTRAIT")
     }
    if (window.matchMedia("(orientation: landscape)").matches) {
      alert("LANSCAPE")
     }
}, false);
ARTniyet
  • 296
  • 1
  • 3
  • 8
7

I disagree with the most voted answer. Use screen and not window

    if(screen.innerHeight > screen.innerWidth){
    alert("Please use Landscape!");
}

Is the proper way to do it. If you calculate with window.height, you ll have trouble on Android. When keyboard is open, window shrinks. So use screen instead of window.

The screen.orientation.type is a good answer but with IE. https://caniuse.com/#search=screen.orientation

Stefdelec
  • 2,711
  • 3
  • 33
  • 40
  • As of Mar 2021, there is no screen.innerHeight ... on Chrome at least. There is a screen.availHeight. And screen.height exists too. – den232 Mar 19 '21 at 10:24
  • Same for Firefox, there is [screen.availHeight](https://developer.mozilla.org/en-US/docs/Web/API/Screen/availHeight) and [screen.availWidth](https://developer.mozilla.org/en-US/docs/Web/API/Screen/availWidth) which [works for all browsers](https://caniuse.com/?search=Screen.avail) – Code4R7 Aug 10 '21 at 09:24
7
$(window).on("orientationchange",function( event ){
    alert(screen.orientation.type)
});
Weedoze
  • 13,683
  • 1
  • 33
  • 63
5

After some experimentation I have found that rotating an orientation aware device will always trigger a browser window's resize event. So in your resize handler simply call a function like:

function is_landscape() {
  return (window.innerWidth > window.innerHeight);
}
Dave Sag
  • 13,266
  • 14
  • 86
  • 134
  • 1
    See above as to why 90 => landscape isn't something you can rely on across different devices. – ChrisF Dec 03 '14 at 00:38
4

iOS doens't update screen.width & screen.height when orientation changes. Android doens't update window.orientation when it changes.

My solution to this problem:

var isAndroid = /(android)/i.test(navigator.userAgent);

if(isAndroid)
{
    if(screen.width < screen.height){
        //portrait mode on Android
    }
} else {
    if(window.orientation == 0){
        //portrait mode iOS and other devices
    }
}

You can detect this change in orientation on Android as well as iOS with the following code:

var supportsOrientationChange = "onorientationchange" in window,
    orientationEvent = supportsOrientationChange ? "orientationchange" : "resize";

window.addEventListener(orientationEvent, function() {
    alert("the orientation has changed");
}, false);

If the onorientationchange event is not supported, the event bound will be the resize event.

Sandman
  • 94
  • 9
3

Get the orientation (at any time in your js code) via

window.orientation

When window.orientation returns 0 or 180 then you are in portrait mode, when returning 90 or 270 then you are in landscape mode.

Sliq
  • 15,937
  • 27
  • 110
  • 143
3

If you have the latest browsers window.orientation might not work. In that case use following code for getting angle -

var orientation = window.screen.orientation.angle;

This is still an experimental technology, you can check the browser compatibility here

Atul Kumar
  • 31
  • 3
2

another alternative to determine orientation, based on comparison of the width/height:

var mql = window.matchMedia("(min-aspect-ratio: 4/3)");
if (mql.matches) {
     orientation = 'landscape';
} 

You use it on "resize" event:

window.addEventListener("resize", function() { ... });
Gregor Srdic
  • 446
  • 7
  • 10
2

Here's the best method I found, based on David Walsh's article (Detect Orientation Change on Mobile Devices)

if ( window.matchMedia("(orientation: portrait)").matches ) {  
   alert("Please use Landscape!") 
}

Explanation:

Window.matchMedia() is a native method that allows you to define a media query rule and check its validity at any point in time.

I find it useful to attach an onchange listener on the return value of this method. Example:

var mediaQueryRule = window.matchMedia("(orientation: portrait)")
mediaQueryRule.onchange = function(){ alert("screen orientation changed") }
Shibl
  • 101
  • 6
2

There's a way by which you can detect if user flipped their device to portrait mode using screen.orientation

Just use the bellow code:

screen.orientation.onchange = function () {
     var type = screen.orientation.type;
     if (type.match(/portrait/)) {
         alert('Please flip to landscape, to use this app!');
     }
}

Now, onchange will get fired when ever user flips the device and alert will pop-up when user using portrait mode.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Naman
  • 1,519
  • 18
  • 32
2
//see also http://stackoverflow.com/questions/641857/javascript-window-resize-event
//see also http://mbccs.blogspot.com/2007/11/fixing-window-resize-event-in-ie.html
/*
Be wary of this:
While you can just hook up to the standard window resize event, you'll find that in IE, the event is fired once for every X and once for every Y axis movement, resulting in a ton of events being fired which might have a performance impact on your site if rendering is an intensive task.
*/

//setup 
window.onresize = function(event) {
    window_resize(event);
}

//timeout wrapper points with doResizeCode as callback
function window_resize(e) { 
     window.clearTimeout(resizeTimeoutId); 
     resizeTimeoutId = window.setTimeout('doResizeCode();', 10); 
}

//wrapper for height/width check
function doResizeCode() {
    if(window.innerHeight > window.innerWidth){
        alert("Please view in landscape");
    }
}
Adolph Trudeau
  • 351
  • 1
  • 8
2

Some devices don't provide the orientationchange event, but do fire the window's resize event:

// Listen for resize changes
window.addEventListener("resize", function() {
    // Get screen size (inner/outerWidth, inner/outerHeight)

}, false);

A bit less obvious than the orientationchange event, but works very well. Please check here

Neuron
  • 5,141
  • 5
  • 38
  • 59
ramya
  • 2,350
  • 6
  • 31
  • 57
1

Instead of 270, it can be -90 (minus 90).

zeuf
  • 400
  • 3
  • 10
1

This expands on a previous answer. The best solution I've found is to make an innocuous CSS attribute that only appears if a CSS3 media query is met, and then have the JS test for that attribute.

So for instance, in the CSS you'd have:

@media screen only and (orientation:landscape)
{
    //  Some innocuous rule here
    body
    {
        background-color: #fffffe;
    }
}
@media screen only and (orientation:portrait)
{
    //  Some innocuous rule here
    body
    {
        background-color: #fffeff;
    }
}

You then go to JavaScript (I'm using jQuery for funsies). Color declarations may be weird, so you may want to use something else, but this is the most foolproof method I've found for testing it. You can then just use the resize event to pick up on switching. Put it all together for:

function detectOrientation(){
    //  Referencing the CSS rules here.
    //  Change your attributes and values to match what you have set up.
    var bodyColor = $("body").css("background-color");
    if (bodyColor == "#fffffe") {
        return "landscape";
    } else
    if (bodyColor == "#fffeff") {
        return "portrait";
    }
}
$(document).ready(function(){
    var orientation = detectOrientation();
    alert("Your orientation is " + orientation + "!");
    $(document).resize(function(){
        orientation = detectOrientation();
        alert("Your orientation is " + orientation + "!");
    });
});

The best part of this is that as of my writing this answer, it doesn't appear to have any effect on desktop interfaces, since they (generally) don't (seem to) pass any argument for orientation to the page.

Josh C
  • 67
  • 10
  • As a note Windows 10 and Edge might be throwing a wrench into this - I haven't done substantial testing on the new platform yet, but at least initially, it seems like it might be supplying orientation data to the browser. It's still possible to build your site around it, but it is definitely something to be aware of. – Josh C Mar 18 '16 at 15:49
1

I used for Android Chrome "The Screen Orientation API"

To look the current orientation call console.log(screen.orientation.type) (and maybe screen.orientation.angle).

Results: portrait-primary | portrait-secondary | landscape-primary | landscape-secondary

Below is my code, I hope it'll be helpful:

var m_isOrientation = ("orientation" in screen) && (typeof screen.orientation.lock == 'function') && (typeof screen.orientation.unlock == 'function');
...
if (!isFullscreen()) return;
screen.orientation.lock('landscape-secondary').then(
    function() {
        console.log('new orientation is landscape-secondary');
    },
    function(e) {
        console.error(e);
    }
);//here's Promise
...
screen.orientation.unlock();
  • I tested only for Android Chrome - ok
Dmytro Sokolyuk
  • 966
  • 13
  • 13
1
screen.orientation.addEventListener("change", function(e) {
 console.log(screen.orientation.type + " " + screen.orientation.angle);
}, false);
zloctb
  • 10,592
  • 8
  • 70
  • 89
  • While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, as this reduces the readability of both the code and the explanations! – Blue Aug 04 '16 at 19:29
  • This answer does not solve the problem in the original question. It shows how to log orientation changes, but it does not show how to display a message only in one particular orientation. – Julian Aug 04 '16 at 21:56
1

The window object in JavaScript on iOS devices has an orientation property that can be used to determine the rotation of the device. The following shows the values window.orientation for iOS devices (e.g. iPhone, iPad, iPod) at different orientations.

This solution also works for android devices as well. I checked in android native browser (Internet browser) and in the Chrome browser, even in the old versions of it.

function readDeviceOrientation() {                      
    if (Math.abs(window.orientation) === 90) {
        // Landscape
    } else {
        // Portrait
    }
}
Dmytro Medvid
  • 4,988
  • 3
  • 25
  • 29
  • window.orientation is Deprecated! [MDN Window.orientation property](https://developer.mozilla.org/en-US/docs/Web/API/Window/orientation) – clay Apr 16 '23 at 23:06
1

This is what I use.

function getOrientation() {

    // if window.orientation is available...
    if( window.orientation && typeof window.orientation === 'number' ) {

        // ... and if the absolute value of orientation is 90...
        if( Math.abs( window.orientation ) == 90 ) {

              // ... then it's landscape
              return 'landscape';

        } else {

              // ... otherwise it's portrait
              return 'portrait';

        }

    } else {

        return false; // window.orientation not available

    }

}

Implementation

window.addEventListener("orientationchange", function() {

     // if orientation is landscape...
     if( getOrientation() === 'landscape' ) {

         // ...do your thing

    }

}, false);
CTE
  • 11
  • 4
  • Your code have a mistake: // if window.orientation is available... if( window.orientation && typeof window.orientation === 'number' ) { the if in and doesn't work if window.orientation=0 – Angelotti Dec 03 '20 at 13:00
1

Thanks to tobyodavies for guiding the way.

To achieve an alert message based on the mobile device's orientation you need to implement the following script within the function setHeight() {

if(window.innerHeight > window.innerWidth){
    alert("Please view in landscape");
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Dan
  • 10,171
  • 7
  • 38
  • 31
0
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title>Rotation Test</title>
  <link type="text/css" href="css/style.css" rel="stylesheet"></style>
  <script src="js/jquery-1.5.min.js" type="text/javascript"></script>
  <script type="text/javascript">
        window.addEventListener("resize", function() {
            // Get screen size (inner/outerWidth, inner/outerHeight)
            var height = $(window).height();
            var width = $(window).width();

            if(width>height) {
              // Landscape
              $("#mode").text("LANDSCAPE");
            } else {
              // Portrait
              $("#mode").text("PORTRAIT");
            }
        }, false);

  </script>
 </head>
 <body onorientationchange="updateOrientation();">
   <div id="mode">LANDSCAPE</div>
 </body>
</html>
0

One thing to note about window.orientation is that it will return undefined if you are not on a mobile device. So a good function to check for the orientation might look like this, where x is window.orientation:

//check for orientation
function getOrientation(x){
  if (x===undefined){
    return 'desktop'
  } else {
    var y;
    x < 0 ? y = 'landscape' : y = 'portrait';
    return y;
  }
}

Call it like so:

var o = getOrientation(window.orientation);
window.addEventListener("orientationchange", function() {
  o = getOrientation(window.orientation);
  console.log(o);
}, false);
Harry Stevens
  • 1,373
  • 1
  • 15
  • 18
0

Or you could just use this..

window.addEventListener("orientationchange", function() {
    if (window.orientation == "90" || window.orientation == "-90") {
        //Do stuff
    }
}, false);
Bradley
  • 132
  • 3
  • 15
  • It's better to add `window.orientation === 90` test egality between number and string is not sure. – Melvynx May 04 '21 at 20:11
0

As you know when device is portrait the width is lower than height of screen.
And when device is landscape the height is lower than width of screen so this will be the code :

var isPortrait = window.innerWidth > window.innerHeight ? false : true;