views:

366

answers:

3

I have an iPhone version of my website. Is it possible to detect, using JavaScript, when the iPhone is shaken? Something along the lines of:

<script> function shaken() { alert("you shaked !!!"); } < /script >

<body onshake="shaken()">
+6  A: 

No, only native apps can access whether it has been shaken or not.

You can implement a native app, by using a wrapper web view with PhoneGap (or other libraries). Such libraries expose motion events and such through a javascript interface.

Update: I mistakenly assumed that orientation doesn't get exposed through javascript. Thanks for the correction.

notnoop
Currently, there is an even to detect the orientation of iphone, but i don't know the one to detect the Shake event.
David
-1 **Wrong.** I've seen web sites that incorporate functionality for sliding left/right on the iPhone. Also, when the orientation changes. See my question here: http://stackoverflow.com/questions/1733254/handle-iphone-events-like-slide-left-in-mobile-web-site
Josh Stodola
Vote reversed :)
Josh Stodola
Thanks Josh! Everyday I learn something new!
notnoop
Josh, your question is very helpful, thanks! but i think there isn't an event like i said...
David
@unknown I added a comment to my answer, please read it.
Josh Stodola
+2  A: 

Check out PhoneGap and Appcelerator's Titanium. Both have JavaScript API's that provide access to things like this.

Darrell Brogdon
Those require packaging the web app as a native app. A random site cannot access PhoneGap or Titanium APIs.
notnoop
i think those are to make iphone native apps, not javascript apis for mobile websites, or not?
David
+1  A: 

See my question here: Handle iPhone Events (like slide-left) in Mobile Web Site

Josh Stodola
It does not appear to support a "onshake" event. I would try to see if shaking the phone would fire `onorientationchange` (this event works on iPhone 1.1+). Keep us posted on the results of your experimentation!
Josh Stodola