开发者

Android 3.0 Web Dev initial orientation issue

开发者 https://www.devze.com 2023-02-22 13:10 出处:网络
I have noticed an issue with the way Android (so far both 2.2 and 3.0) sets the window.orientation javascript variable. When you initially load a web page everything works fine window.orientation is c

I have noticed an issue with the way Android (so far both 2.2 and 3.0) sets the window.orientation javascript variable. When you initially load a web page everything works fine window.orientation is correctly set to the orientation the device is in (0, 90, -90, 180). The problem arises when you launch a new page via a window.open("newpage") javascript call. When the new page opens the window.orientation value is always 0 no matter what orientation you are in. Below is a sample webpage that can demonstrate this:

<button onclick="window.open('AndroidTesting.html')">Click Me</button>

    <script type="text/javascr开发者_运维问答ipt">

    orientationEvent = "resize";
    window.addEventListener(orientationEvent, function () {
        setTimeout(displayChange(), 0);
    }, false);

        var myPageWidth = 0;
        var myPageHeight = 0;

        var lastOrientation = "Landscape";

        function displayChange() {

            if ((window.orientation == 90) || (window.orientation == -90)) {
                lastOrientation = "Portrait";
            }
            if ((window.orientation == 0) || (window.orientation == 180)) {
                lastOrientation = "Landscape";
            }

            document.getElementById('storydiv').innerHTML = "In " + lastOrientation + " mode";

        }

    </script>

When you click the button to open the same page in a new tab the orientation is wrong if you are not in the default mode (Android 2.2 the default mode or 0 angle is portrait in Android 3.0 the 0 angle is landscape), but once you rotate to the other mode and then back the orientation works.

Has anyone seen this issue before? Is there a work around, or is it just a problem because I am doing something wrong?

Thanks for the help!


I don't know if it's a good approach, but it seems that on Android, screen.width and screen.height depends of the device orientation.

if(android){
    if(screen.width>screen.height){
        return 90;
    }else{
        return 0;
    }
}else{
    return window.orientation;
}

The main problem of this solution is in the os detection.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号