Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
194
rated 0 times [  198] [ 4]  / answers: 1 / hits: 24836  / 13 Years ago, mon, march 7, 2011, 12:00:00

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.


More From » ios

 Answers
65

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:


[#93396] Friday, March 4, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janettejordynm

Total Points: 550
Total Questions: 94
Total Answers: 98

Location: Senegal
Member since Fri, Aug 21, 2020
4 Years ago
janettejordynm questions
Tue, Nov 24, 20, 00:00, 4 Years ago
Sat, May 23, 20, 00:00, 4 Years ago
Mon, Apr 6, 20, 00:00, 4 Years ago
Tue, Feb 18, 20, 00:00, 4 Years ago
;