dSquared
dSquared

Reputation: 9825

Android window.orientation incorrect on start-up using JavaScript + Phonegap

I am writing a web app using HTML/JavaScript/Phonegap 1.4.1 for Android devices (target API Level = 10).

I'm trying to use JavaScript to detect the device orientation on initial app start-up, however the window.orientation method is displaying incorrect values.

For example, with the device in landscape mode, running the following code illustrates the issue:

$(document).ready(function(){
    if (typeof navigator.device == undefined){
        document.addEventListener('deviceready', test, false);
    } else {
        test();
    }
});

function test() {
    console.log(window.orientation); // Displays 0
    var tm = setTimeout("console.log(window.orientation)", 2000); // Displays 90
});

Any help would be greatly appreciated;

Thank you!

Upvotes: 1

Views: 833

Answers (1)

gmh04
gmh04

Reputation: 1351

The check if (typeof navigator.device == undefined) is going to return false before the deviceready fires. The following should work:

$(document).ready(function()
    document.addEventListener('deviceready', test, false);
});

function test() {
    console.log(window.orientation);
});

Upvotes: 1

Related Questions