Android y cambio de orientación

I have this bit of jQuery:

$('body').bind('orientationchange',function(event){
alert('orientationchange');
})

If you change the orientation of your device, it should trigger an alert. Works in iOS. Does not work in Android. Why?

JSBIN: http://jsbin.com/esacir

I thought perhaps Android doesn't fire that event on the body so also tried 'window': http://jsbin.com/esacir/3 still no-go.

preguntado el 09 de marzo de 12 a las 23:03

2 Respuestas

Esto funciona en Android:

window.onorientationchange = function() {
    alert('onorientationchange');
}

Try declaring the method as done above.

respondido 09 mar '12, 23:03

It's odd that I can't bind that event via jQuery, though. - DA.

you get the answer. The issue is that android needs the event on the window, rather than body. I just had sloppy syntax when I tried it. - DA.

As usual with me, it was a syntax problem. The key is that the event has to be attached to the window (for android):

$(window).bind('orientationchange',function(event){
alert('orientationchange');
})

respondido 10 mar '12, 00:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.