Alex Wayne
Alex Wayne

Reputation: 187004

iPhone webapp: run JavaScript on orientation change

I have a webapp saved to the iPhone homescreen, and I need to run some JavaScript when the device changes orientation.

I tried this, but it only seemed to work in a desktop browser and not on the iPhone.

window.onresize = function() {
  alert('rotation change!');
};

Is there some special event I'm supposed to hook into?

Upvotes: 1

Views: 1047

Answers (2)

dimme
dimme

Reputation: 4424

Use this together with jQuery:

$(document).ready(function () {
  function reorient(e) {
    alert('rotation change!');
  }
  window.onorientationchange = reorient;
  window.setTimeout(reorient, 0);
});

Or you can just use onorientationchange in your code.

You don't need to use jQuery in case you dont need to know when the DOM document is ready.

Upvotes: 0

SLaks
SLaks

Reputation: 887305

You're looking for onorientationchange.

Upvotes: 3

Related Questions