如何处理 uiwebview 内的 Javascript onorientationchange 事件
任何人都可以帮助我在 iPhone 中通过 uiwebview 访问时如何处理 Javascript 中的方向更改事件?
我正在尝试捕获 Javascript 的 onorientationchange
事件。我尝试了以下代码:
<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 访问页面时,方向更改事件没有被捕获,并且无法实现我想要的功能。
Anyone could help me how to handle orientation change event in Javascript when visiting through uiwebview
in iPhone?
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>
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 achieved.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我想向您指出 这篇文章:“iphone- uiwebview-local-resources-using-javascript-and-handling-onorientationchang”,接受答案正下方的答案确实对您的问题表示敬意:处理应用程序代码中的方向更改并使用 javascript 注入更改。我引用希望作者不会介意:
“对 UIWebView 中未调用 onorientationchange 处理程序的第二个问题的回答:
我注意到 window.orientation 属性无法正常工作,并且 window.onorientationchange 处理程序没有得到大多数应用程序都会遇到此问题。这可以通过设置 window.orientation 属性并在包含 UIWebView 的视图控制器的 willRotateToInterfaceOrientation 方法中调用 window.onorientationchange 来解决:”
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:
"An answer to your second problem of the onorientationchange handler not being called in UIWebView:
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:"
感谢你。
另一种写法是:
Thank u.
Another way to write this is:
将其放入
viewDidLoad
中:现在您可以以正确的方向调用您的 webview :)
Put this in
viewDidLoad
:Now you can call your webview with the right orientation :)