Google analytics code

Friday, January 25, 2013

jQuery / Javascript: Detecting orientation and updating for it

I'm getting started on a new mobile project at work. The first step is playing with the orientation of the device and updating the layout so it takes advantage of the available space.

The javascript event you're looking for is orientationchange. You can watch for this with the jQuery on method.


The handler method will look at the window.orientation to figure out if it's portrait or landscape.

if(Math.abs(window.orientation) === 90){
// landscape: do stuff
// portrait: do stuff

You can see an example here.