Monday, August 27, 2012

Detect orientation change using Javascript

Example to detect orientation change using Javascript in browser.

Detect orientation change using Javascript


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name = "viewport" content = "width = device-width, height = device-height" />
<title>Mobile-Web-App: Detect Orientation Change</title>
<script type="text/javascript">

function doOnLoad(){
 var supportOrientationChange = "onorientationchange" in window;
 
 if(supportOrientationChange){
  document.getElementById("browserinfo").innerHTML 
   = "onorientationchange supported";
  
  window.addEventListener(
   "orientationchange", 
   function() {  
    document.getElementById("browserinfo").innerHTML 
     = window.orientation + " : " + screen.width + " x " + screen.height;
    }, 
   false);
 }else{
  document.getElementById("browserinfo").innerHTML 
   = "onorientationchange NOT supported";
 }
 

}

</script>
</head>
<body onload="doOnLoad();">
<h1>Mobile-Web-App: Javascript Exercise - Detect Orientation Change</h1>
<div id="browserinfo"></div>
</body>
</html>


No comments:

Post a Comment