V-Xtreme
V-Xtreme

Reputation: 7333

Working with orientation of the iPad in javascript

I am working with the orientation in iPad . I am using phone gap for building the application. I have done following code for orientation change :

       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 am getting the alerts but I don't know how to resize the window inside this ? Can anybody help me ?I want like following : enter image description here

Upvotes: 1

Views: 483

Answers (1)

MyBoon
MyBoon

Reputation: 1298

Both Safari and Android support a simple orientation change 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

Upvotes: 2

Related Questions