Search code examples
javascriptcssipadaccelerometergalaxy-tab

Detect change in orientation using javascript


Is it possible to detect change in orientation of the browser on the iPad or Galaxy Tab using javascript? I think it's possible using css media queries.


Solution

  • NOTE: orientationChange is deprecated

    Instead use screen.orientation using the screenOrientation interface

    which is triggered by the screenorientation.onchange event

    window.addEventListener("DOMContentLoaded", () => {
      const output = document.getElementById("o9n");
      const displayOrientation = () => {
        const screenOrientation = screen.orientation.type;
        output.innerHTML = `The orientation of the screen is: ${screenOrientation}`;
        if (screenOrientation === "landscape-primary") {
          console.log("That looks good.");
        } else if (screenOrientation === "landscape-secondary") {
          console.log("Mmmh... the screen is upside down!");
        } else if (screenOrientation === "portrait-secondary" || screenOrientation === "portrait-primary") {
          console.log("Mmmh... you should rotate your device to landscape");
        } else if (screenOrientation === undefined) {
          console.log("The orientation API isn't supported in this browser :(");
        }
      };
    
      if (screen && screen.orientation !== null) {
        try {
          window.screen.orientation.onchange = displayOrientation;
          displayOrientation();
        }
        catch (e) { output.innerHTML = e.message; }
      }
    });
    Orientation: <span id="o9n"></span>

    However note the support as of July 2022

    The screen.orientation is not supported by Safari at all

    enter image description here


    MDN:

    window.addEventListener("orientationchange", function() {
        alert("the orientation of the device is now " + screen.orientation.angle);
    });
    

    or jQuery mobile orientationchange

    $(window).on("orientationchange", function( event ) {
      $("#orientation").text( "This device is in " + event.orientation + " mode!");
    });
    

    The older orientationChange and window.orientation should still work for Safari

    window.addEventListener("orientationchange", function() {
      alert(window.orientation);
    }, false);
    

    Older answer

    http://www.nczonline.net/blog/2010/04/06/ipad-web-development-tips/

    Safari on the iPad does support the window.orientation property, so if necessary, you can use that to determine if the user is in horizontal or vertical mode. As reminder of this functionality:

    window.orientation is 0 when being held vertically
    window.orientation is 90 when rotated 90 degrees to the left (horizontal)
    window.orientation is -90 when rotated 90 degrees to the right (horizontal)
    

    There is also the orientationchange event that fires on the window object when the device is rotated.

    You can also use CSS media queries to determine if the iPad is being held in vertical or horizontal orientation, such as:

    <link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
    <link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
    

    http://www.htmlgoodies.com/beyond/webmaster/toolbox/article.php/3889591/Detect-and-Set-the-iPhone--iPads-Viewport-Orientation-Using-JavaScript-CSS-and-Meta-Tags.htm

    <script type="text/javascript">
    var updateLayout = function() {
      if (window.innerWidth != currentWidth) {
        currentWidth = window.innerWidth;
        var orient = (currentWidth == 320) ? "profile" : "landscape";
        document.body.setAttribute("orient", orient);
        window.scrollTo(0, 1);
      }
    };
    
    iPhone.DomLoad(updateLayout);
    setInterval(updateLayout, 400);
    </script>