Is it possible to listen for an iPhone (or any mobile phone with accelerometer) shake event with JavaScript? You know like shaking the iPhone to shuffle the next song in iTunes I want to call a JS function on my website when the iPhone got shaken.
Asked
Active
Viewed 1.2k times
4 Answers
16
In the latest iOS update (4.2) the accelorometer is now accessible from Javascript.
See: http://www.mobilexweb.com/blog/safari-ios-accelerometer-websockets-html5

Olly
- 500
- 4
- 8
10
Here's an elegant implementation of shake detection using the accelerometer if anyone is looking for it:

bmargulies
- 97,814
- 39
- 186
- 310

Mr Speaker
- 3,047
- 24
- 17
4
I just implemented the above in my web app which now has controls that appear on the page only after a device shake occurs. Very easy to implement and cleanly described.
https://github.com/alexgibson/shake.js
It is also worth noting that the iOS simulator's shake gesture doesn't trigger the event but a genuine device shake does.

Alex Lockwood
- 83,063
- 39
- 206
- 250

Mat
- 41
- 1