Safari on iPhone & iPad 4.2: Accelerometer, WebSockets & better HTML5 support

iOS 4.2 is a free update for every iPhone, iPod or iPad device available now. This new release provides some major changes on HTML5 and W3C future standards support, like WebSockets and Accelerometer support, print support, new JavaScript data-types and better SVG support.

Apple didn’t update yet Safari documentation to reflect these new APIs. This information is based on JavaScript research and testing over Safari itself I’ve been doing. The list of new features I’ve detected are:

  • Accelerometer & Gyroscope support through the DeviceOrientation API
  • WebSockets API from HTML5
  • Updated HTML5 Form Support
  • Partial XHR-2 Support
  • Print Support
  • New JavaScript data types
  • New DOM events
  • Enhanced SVG and Canvas support

Accelerometer & Gyroscope support

As you may know, all iOS devices have accelerometer sensors (plusmagnetometer and gyroscope on some devices). However, as web developers, we didn’t have access to such sensors until now. Safari now supports the DeviceOrientation API (W3C draft). Looking at the available objects, it seems that all the API is fully supported (including ondeviceorientation and ondevicemotion events); I could only get accelerometer data with success.

UPDATE: Apple has just released the API documentation: accelerometer and gyroscope supported. See DeviceMotionEvent Class Reference and DeviceOrientation Class Reference.

If you have an iOS 4.2 device, go to from Safari browser. I’ve coded a sample in 15 minutes using JavaScript and some CSS3: it’s the typical ball moving on the screen regarding the iOS device’s position. The next video shows this sample in action:

The API detects and delivers accelerometer data 50 times per second. To get them you need to capture ondevicemotion event on the window global object (or using addEventListener with devicemotion as the event name) and then use the accelerationIncludingGravity property on the DeviceOrientationEvent parameter. It has three values, x, y & z, representing the acceleration in m/s2 for each axis. You should use typical accelerometer math for games, effects or CSS transformations.

UPDATE: If the device has gyroscope (iPhone 4 or iPod Touch 4G) then you can use acceleration instead of accelerationIncludingGravity.

window.ondevicemotion = function(event) {
// event.accelerationIncludingGravity.x
// event.accelerationIncludingGravity.y
// event.accelerationIncludingGravity.z

UPDATE: For gyroscope, you need to capture ondeviceorientation and read alpha, beta and gamma properties from the event parameter, giving us angles (between 0 and 360) for detecting rotation changes.  Remember that not all iOS devices supports gyroscope, so you will not receive this kind of events on iPod before 4G, iPad or iPhone 3GS or older.

window.ondeviceorientation = function(event) {
// event.alpha
// event.beta
// event.gamma


The other big new update is WebSockets support. WebSockets is a W3C HTML5 API currently in draft that allows JavaScript to use an open, bi-directional full-duplex connection to a server using TCP sockets. This is a great news for chat and real-time applications that will reduce AJAX periodic calls.

You will need a web server understanding the new WS protocol through an HTTP handshake. You should always rely on a fallback mechanism if WS is not supported on the server, or because of a proxy/gateway.

HTML5 Form Support

Besides the HTML5 Form support I’ve already discussed on the book, now we have support for the required boolean attribute and the new :invalid CSS pseudoclass. Therefore, the following code will show a green input text when completed and a yellow one when incomplete:

input {
background-color: green;
color: white;
input:invalid {
background-color: yellow;
<input type="text" required>


The W3C draft called XMLHttpRequest Level 2 (aka AJAX 2) adds new features to the XHR object and functionality. From that specification, now Safari supports the FormData object that allow us to send form data via AJAX easily.

Print Support

iOS 4.2 includes AirPrint, a wireless printing solution. Therefore, we can use now window.print() to invoke the printing dialog on Safari.

New JavaScript Data-types

Safari now supports the Blob class and many integer-type collections, like Float32Array, Int8Array, Uint8Array, Int16Array Uint16Aray, Int32Array and UInt32Array defined on Typed Arrays specification. More information on Firefox website.

New DOM events

Besides the new motion events, now we can use the HTML5 new onhashchange event that detects changes on the URL after the hash (#) for AJAX-like webapps; the invalid, onbeforeload and onpopstate events from HTML5 draft specification.

UPDATE: It appears that Event Source API (W3C draft) is also implemented through the available EventSource class to receive server-side events.

UPDATE: I’ve receive some comments about onhashchange being supported from 4.0 or 4.1 versions.

Now, we can also use window.captureEvents and window.releaseEvents to capture events in a global way.

Enhanced SVG and Canvas support

iOS supports SVG as a separate document and also inline SVG (using the svg tag). And now we can also create SVG documents on the fly using a list of more than 20 classes SVG____, like SVGDocument, SVGImage directly from our code.
From HTML5 Canvas, there is now support for ImageData data-type, a way to manipulate images pixel by pixel from JavaScript.

Other stuff

  • A styleMedia API that allow us to make CSS Media Queries from JavaScript. See API.
  • A WebGLRenderingContext class available, part of the 3D Drawing API (aka WebGL). However, I’m not seeing any real WebGL support.
  • A global RGBColor constructor

I will continue testing new HTML5 features and APIs available in this new release. Do you know any other new feature? Feel free to contact me by twitter (@firt) or commenting this post.


159 thoughts on “Safari on iPhone & iPad 4.2: Accelerometer, WebSockets & better HTML5 support

  1. Can anyone tell me why my orientation button now only turns off music. Is there another button that I don’t know about. Please help

  2. Can anyone tell me why my orientation button now only turns off music. Is there another button that I don’t know about.

  3. You’re missing a very key element that they added in that nobody is covering; they allow local cache to extend to 10mb from 5mb now letting image-centric web apps run completely offline.

  4. Pingback:
  5. Repost from yesterday –>

    Here’s another modified version of the accelerometer demo with the moving ball. Based on the mods by @Andrea. I’ve optimized the JavaScript and added CSS3 xforms which are hardware accelerated on the iPhone/iPad.

  6. I’m curious if there is a way to access the accelerometer to override screen rotate. For instance: If you have a page designed to fit into the Safari browser window specifically in portrait view. And you don’t want the page to change orientation when the iPhone is rotated. I’m not talking about a user-initiated disabling. Something that can be done on a specific page. Seems like you could look at the accelerometer data and negate the rotation. Any thoughts?

  7. Chris,

    There is no way to do that (override screen rotate) up to now. It should be implemented with a Meta tag by browsers because the screen rotation happens on the native side. There are some hacks, like rotating the main content using CSS 3 transformations, but the Safari UI is rotated anyway and generates a lot of problems.

    You can always embed your webapp in a native app (hybrid) and force one orientation. Check Chapter 12 of Programming the Mobile Web for info about that or I can send you some ideas. If you need a discount code for the book, just ask me.

Leave a Reply

Your email address will not be published. Required fields are marked *