JavaScript Orientationchange event

One of the JavaScript events FIT 14 offers is the orientationchange event. This event will be triggered every time the viewport changes from portrait to landscape or vice versa.

The requirements for an orientationchange to happen are:

  • The DC property viewport/resizable is not false.
  • Landscape orientation will only be recognized if the landscape viewport width is greater than the portrait viewport width.
  • In the conf/config.xml the js-api/orientation option needs to be enabled:
<config>
  <js-api> <orientation /> </js-api> </config>

Adding a listener

A listener can be added using the ai.addEventListener function. The eventType is orientationchange.

ai.addEventListener('orientationchange', function(e) {
  alert('Your current orientation is:' + e.orientation);
});

Event object

The object passed to the listener function has the following properties:

  • orientation
    • String, the orientation of the viewport after the orientation changed.
  • width
    • Integer, the width of the viewport after the orientation changed.
  • height
    • Integer, the height of the viewport after the orientation changed.