Working with iPad orientation in javascript

I work with orientation in iPad. I am using a telephone pledge to create an application. I made the following code to change the orientation:

       function updateOrientation() 
        {
            var orientation=window.orientation;
            if ( orientation == 0 ) {
                alert("Do Something In Portrait Mode");
            }
            else if ( orientation == 90 ) {
                alert("Do Something In landscape Mode");
            }
            else if ( orientation == -90 ) {
                alert("Do Something In Portrait Mode");
            }
            else if ( orientation == 180 ) {
                alert("Do Something In landscape Mode");
            }               
        }

I get warnings, but I don’t know how to resize the window inside this? Can anybody help me? enter image description here

+1
source share
1 answer

Both Safari and Android support a simple orientation event listener.

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>PhoneGap</title>
     <script type="text/javascript" charset="utf-8" src="phonegap-1.1.0.js"></script>
     <script type="text/javascript" charset="utf-8" src="main.js"></script>

</head>
<body onload="init();" >

    <h2>Orientation Test</h2>
    <div id="status"></div>

</body>
</html>

And js code:

function init() {
    window.addEventListener("orientationchange", orientationChange, true);
}

function orientationChange(e) {
    var orientation="portrait";
    if(window.orientation == -90 || window.orientation == 90) orientation = "landscape";
        document.getElementById("status").innerHTML+=orientation+"<br>";
}

Source

+2
source

Source: https://habr.com/ru/post/1761108/


All Articles