如何在uiwebview中处理javascript onorientationchange事件

任何人都可以帮助我在iphone中通过uiwebview访问时如何处理javascript中的方向更改事件? 我想捕获
onorientationchange
javascript的事件。我试过以下代码。
<style type="text/css" media="only screen and (max-device-width: 480px)">
    .portrait
    {
        width: 300px;
        padding: 0 15px 0 5px;
    }
    .landscape
    {
        width: 460px;
        padding: 0 15px 0 5px;
    }
</style>

<script type="text/javascript">
    window.onload = orientationchange;
    window.onorientationchange = orientationchange;
    function orientationchange() {
        if (window.orientation == 90
            || window.orientation == -90) {
            document.getElementById('contents').className = 'landscape';
        }
        else {
            document.getElementById('contents').className = 'portrait';
        }
    }
</script>

// and I have a div inside html

<div id="contents">
    my contents and description... e.t.c, and e.t.c...
</div>
它在safari中工作正常但是当我使用uiwebview访问页面时,没有捕获方向更改事件,并且无法实现我想要的功能。     
已邀请:
我想向您指出这篇文章:“iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang”,答案直接在接受的答案下方确实对您的问题表示敬意:处理应用程序中的方向更改使用javascript编写代码并注入更改。我引用希望作者不介意: “在UIWebView中没有调用onorientationchange处理程序的第二个问题的答案: 我注意到window.orientation属性不能正常工作,并且不会调用window.onorientationchange处理程序。大多数应用程序遇到此问题这可以通过设置window.orientation属性并在包含UIWebView的视图控制器的willRotateToInterfaceOrientation方法中调用window.onorientationchange来修复:     
感谢你。 另一种写这个的方法是:
var onorientationchange = function()    {
    document.getElementById('contents').className =
      Math.abs(window.orientation == 90)? "landscape" : "portrait";
}
window.onload = function() {
    window.onorientationchange = window.onorientationchange
}
    
把它放在
viewDidLoad
[[UIDevice currentDevice]beginGeneratingDeviceOrientationNotifications];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hasOrientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

- (void)hasOrientationChanged:(NSNotification *)notification {
    UIDeviceOrientation currentOrientation; 
    currentOrientation = [[UIDevice currentDevice] orientation];
    if(currentOrientation == UIDeviceOrientationLandscapeLeft){
        NSLog(@"left");
    }

    if(currentOrientation == UIDeviceOrientationLandscapeRight){
        NSLog(@"right");
    }

}
现在你可以用正确的方向调用你的webview :)     

要回复问题请先登录注册