如何在uiwebview中处理javascript onorientationchange事件 [英] how to handle javascript onorientationchange event inside uiwebview

查看:150
本文介绍了如何在uiwebview中处理javascript onorientationchange事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我在iphone中通过uiwebview访问时如何处理javascript中的方向更改事件?

Anyone could help me how to handle orientation change event in javascript when visiting through uiwebview in iphone?

我正在尝试捕获 onorientationchange javascript事件。我尝试了以下代码。

I am trying to capture onorientationchange event of javascript. I tried following code.

<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访问页面时,没有捕获方向更改事件我想要的功能无法实现。

It works fine in safari but when I visit the page using uiwebview the orientation change event is not being captured and my desired functionality could not being acheived.

推荐答案

我想指出这篇文章:iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang ,接受答案正下方的答案确实对您的问题表示敬意:处理应用程序代码中的方向更改并使用javascript注入更改。我引用希望作者不介意:

I'd like to point you to this post: "iphone-uiwebview-local-resources-using-javascript-and-handling-onorientationchang", the answer directly below the accepted answer does pay tribute to your question: Handle orientation changes in application code and inject changes with javascript. I am quoting in hope the author won't mind:

你在UIWebView中没有调用onorientationchange处理程序的第二个问题的答案:

"An answer to your second problem of the onorientationchange handler not being called in UIWebView:

我注意到window.orientation属性无法正常工作,并且没有调用window.onorientationchange处理程序。大多数应用程序都遇到了这个问题。这可以通过设置window.orientation属性来解决。在包含UIWebView的视图控制器的willRotateToInterfaceOrientation方法中调用window.onorientationchange:

I noticed that the window.orientation property does not work properly and the window.onorientationchange handler does not get called. Most apps suffer this problem. This can be fixed by setting the window.orientation property and calling window.onorientationchange in the willRotateToInterfaceOrientation method of the view controller that contains your UIWebView:"

这篇关于如何在uiwebview中处理javascript onorientationchange事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆