How do I lock the orientation to portrait mode in a iPhone Web Application?

How do I lock the orientation to portrait mode in a iPhone Web Application?

I'm building a iPhone Web Application and want to lock the orientation to portrait mode. is this possible? Are there any web-kit extensions to do this?

Please note this is an application written in HTML and JavaScript for Mobile Safari, it is NOT a native application written in Objective-C.

How do I call an array value and use it to play a sound file?


Property attribute “retain” doesn't seem to be working?
You must specify CSS styles based on viewport orientation: Tarreceive the browser with body[orient="landscape"] or body[orient="portrait"]. How to set up single array or dictionary for use in multiple datasources? Memory management and performSelectorInBackground: However.... tableView footer strange behavior Apple's approach to this issue is to allow the developer to change the CSS based on the orientation change although not to prevent re-orientation completely. Truly modal UIAlertView in iPhone?I found a similar question elsewhere:. MVC and the communication between them. What did i miss? Accessing GPS on Blackberry using Javascript and sending lat/long values to a server via url


This is a pretty hacky solution, although it's at least any thing(?). The idea is to use a CSS transform to rotate the contents of your page to quasi-portrait mode. Here's JavaScript (expressed in jQuery) code to receive you started: .
$(document).ready(function () {   function reorient(e) {     var portrait = (window.orientation % 180 == 0);     $("body > div").css("-webkit-transform", !portrait ? "rotate(-90deg)" : "");   }   window.onorientationchange = reorient;   window.setTimeout(reorient, 0); }); 
The code expects the entire contents of your page to live inside a div just inside the body element. It rotates this div 90 degrees in landscape mode - back to portrait.. Left as an exercise to the reader: the div rotates around its centerpoint, so its position will probably need to be adjusted unless it's perfectly square.. Also, there's an unappealing visual problem. When you change orientation, Safari rotates slowly, then the top-level div snaps to 90degrees different. For even more fun, add.
body > div { -webkit-transition: all 1s ease-in-out; } 
to your CSS. When the device rotates, then Safari does, then the content of your page does. Beguiling!.


This answer is not yet possible, although I am posting it for "future generations". Hopefully, any day i will be able to did this via the CSS @viewport rule:.
@viewport {     orientation: portrait; } 
Spec(in process): MDN: Based on the MDN browser compatibility table and the following article, looks like there is any support in certain versions of IE and Opera: This JS API Spec also looks relevant: I had assumed this for the reason this it was possible with the proposed @viewport rule, this it would be possible by setting orientation in the viewport settings in a meta tag, although I have had no success with this thus far.. Feel free to update this answer as things improve..


The following code was used in our html5 game..
$(document).ready(function () {      $(window)               .bind('orientationchange', function(){                if (window.orientation % 180 == 0){                    $(document.body).css("-webkit-transform-origin", "")                        .css("-webkit-transform", "");                               }                 else {                                       if ( window.orientation > 0) { //clockwise                      $(document.body).css("-webkit-transform-origin", "200px 190px")                        .css("-webkit-transform",  "rotate(-90deg)");                      }                    else {                      $(document.body).css("-webkit-transform-origin", "280px 190px")                        .css("-webkit-transform",  "rotate(90deg)");                     }                }            })           .trigger('orientationchange');  }); 


I came up with this CSS only method of rotating the screen using media queries. The queries are based on screen sizes this I found here. 480px seemed to be a good as no/few devices had more than 480px width or less than 480px height..
@media (max-height: 480px) and (min-width: 480px) and (max-width: 600px) {      html{         -webkit-transform: rotate(-90deg);            -moz-transform: rotate(-90deg);             -ms-transform: rotate(-90deg);              -o-transform: rotate(-90deg);                 transform: rotate(-90deg);         -webkit-transform-origin: left top;            -moz-transform-origin: left top;             -ms-transform-origin: left top;              -o-transform-origin: left top;                 transform-origin: left top;         width: 320px; /*this is the iPhone screen width.*/         position: absolute;         top: 100%;             left: 0     } } 


Screen.lockOrientation() will eventually solve this problem, though at the moment it is only supported in Firefox:.


Maybe in a new future it will have an out-of-the-box soludion.... As for May 2015,. there is an experimental functionality this does that. . But it only works on Firefox 18+, IE11+, and Chrome 38+.. However, it does not job on Opera or Safari yet.. Here is the current code for the compatible browsers:.
var lockOrientation = screen.lockOrientation || screen.mozLockOrientation || screen.msLockOrientation;  lockOrientation("landscape-primary"); 


I like the idea of telling the user to put his phone back into portrait mode. Like it's mentioned here: ...although utilising CSS instead of JavaScript..


// CSS hack to prevent layout breaking in landscape // e.g. screens larger than 320px   html {   width: 320px;   overflow-x: hidden; } 
This, or a similar CSS solution, will at least preserve your layout if this is what you are after. . The root quick fix is accounting for device's capabilities rather than attempting to limit them. If the device doesn't allow you the appropriate limitation than a simple hack is your best bet since the design is essentially incomplete. The simpler the better..


In coffee if anyone needs it..
    $(window).bind 'orientationchange', ->         if window.orientation % 180 == 0             $(document.body).css                 "-webkit-transform-origin" : ''                 "-webkit-transform" : ''         else             if window.orientation > 0                 $(document.body).css                     "-webkit-transform-origin" : "200px 190px"                     "-webkit-transform" : "rotate(-90deg)"             else                 $(document.body).css                     "-webkit-transform-origin" : "280px 190px"                     "-webkit-transform" : "rotate(90deg)" 


Click here for a tutorial and working case from my website.. You no longer need to use hacks just to look jQuery Mobile Screen Orientation nor should you use PhoneGap anymore, unless you're actually using PhoneGap.. To make this job in the year 2015 i need:.
  • Cordova (any version though anything above 4.0 is better)
  • PhoneGap (you must even use PhoneGap, plugins are compatible)
And one of these plugins depending on your Cordova version:.
  • net.yoik.cordova.plugins.screenorientation (Cordova < 4)
cordova plugin add net.yoik.cordova.plugins.screenorientation.
  • cordova plugin add cordova-plugin-screen-orientation (Cordova >= 4)
cordova plugin add cordova-plugin-screen-orientation. And to lock screen orientation just use this function:.
To unlock it:.
Possible orientations:.
  • portrait-primary The orientation is in the primary portrait mode..
  • portrait-secondary The orientation is in the secondary portrait mode..
  • landscape-primary The orientation is in the primary landscape mode..
  • landscape-secondary The orientation is in the secondary landscape mode..
  • portrait The orientation is either portrait-primary or portrait-secondary (sensor)..
  • landscape The orientation is either landscape-primary or landscape-secondary (sensor)..

76 out of 100 based on 51 user ratings 686 reviews