The <body> onorientationchange is used to listen to the orientation change even. Whenever it is fired, the 'content' meta will be reconfigured according to the orientation status. Javascript's window.orientation is used to check the current orientation status in terms of degrees.
Let's jump into the codes:
<html> <head> <title>Javascript to Limit HTML Width for Mobile Devices of Any Orientation</title> <script> var mobile = navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/iPhone|iPad|iPod/i) || navigator.userAgent.match(/Opera Mini/i) || navigator.userAgent.match(/IEMobile/i); var safari = navigator.userAgent.match(/iPhone|iPad|iPod/i) && navigator.userAgent.match(/Safari/i); var ot = ""; // variable "orientation" is used by mobile browsers if (safari) { if (window.orientation == 0) { ot="portrait"; } else { ot="landscape"; } } else { if (screen.width<screen.height) { ot="portrait"; } else { ot="landscape"; } } if (mobile) { var mobileWidth = getMobileWidth(); alert("setting the width of "+mobileWidth); document.write('<meta id="vp" name="viewport" content="width = '+mobileWidth+', initial-scale=1.0">'); } // if it is not mobile, viewport is not supported or will be ignored function getMobileWidth() { var width; var isTablet = 0; if (safari) { // safari screen width and height remain constant irregradless of the orientation if (ot == "portrait") { width = screen.width; if (width>400) {isTablet=1;} } else { width = screen.height; if (width>700) {isTablet=1;} } } else { width = screen.width; if (ot == "portrait") { if (width>400) {isTablet=1;} } else { if (width>700) { isTablet=1;} } } if (isTablet) { if (ot == "portrait") { return 400; } else { return 700; } } else { return width; } } function changeViewPort() { //change viewport dynamically by changing the meta 'content' attribute if (safari) { // mobile safari will not swap screen.width with screen.height after orientation change if (window.orientation == 0) { ot="portrait"; } else { ot="landscape"; } } else { // other broswers will have height and width swapped if (screen.width<screen.height) { ot="portrait"; } else { ot="landscape"; } } var mobileWidth = getMobileWidth(); alert("setting the width of "+mobileWidth); alert("screen in "+ot+" mode w:"+screen.width+" h:"+screen.height+" -->"+window.orientation); if (mobile) { var vp = document.getElementById('vp'); vp.setAttribute('content','width='+mobileWidth+', initial-scale=1.0'); } } </script> </head> <body bgcolor=orange onorientationchange="changeViewPort()"> <table width=100% height=100% align=center valign=middle> <tr><td width=100% height=100% align=center valign=middle style="font-family: Lucida Sans, Lucida Sans Unicode, Helvetica, Arial, sans-serif;">Under Construction</td></tr> </table> </body> </html> |
Please note the meta tag has been given an ID (id="vp") so that it can be modified when necessary. onorientationchange is used instead of "onresize" as "onresize" will make give a lots of false signal even when the address bar in mobile browsers shows up or hides.
No comments:
Post a Comment