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. Any word on changes to how the iPad and iOS4.2 handles html5 video? We’ve been using an autoplay fix / hack that has stopped working.

  2. What about CSS position: fixed, overflow scroll or frames?

    You know, I’m really in doubt of the merits of apple implementing html5 when it doesn’t even manage to implement plain old CSS 2.0 and HTML 4.0. Aproximately correctly.

  3. Regarding the orientation lock – now you “can” – just detect the orientation and when in the position, when iPhone rotates 90°, rotate the body -90% -webkit-transform:rotate(-90deg) and vice versa.

  4. Great post will have plenty to play with now. One thing though EventSource has been supported since iOS4.0 so it’s not new for 4.2.

  5. I got a bunch of JavaScript/ PhoneGap based games out for the iPad and testing one of them on my new iOS4.2 install, it seems like it runs faster (well, my timer is fixed, but there are no small lags anymore when the screen is very busy with sprites). Nice! (Side-note: this change also means one needs to remember to test on both iOS if there is speed-crucial JS.)

    Caveat: This speed comparison is by no means scientific — I just checked my app and activated as many sprites as I could and didn’t get any lags. Anyone wants to do a real speed test of drawing sprites for old iOS vs new iOS on iPad?

  6. I am noticing that playback through and tags has to be use-action initiated. You can no longer use javascript to start playback through ajax or something like setTimeout. @font-face seems to work better, though.

  7. input:invalid {background-color: yellow;}

    Maybe Apple knows something we don’t!

    They like one character top-level domains names.

  8. <style>
    input:invalid {background-color: yellow;}
    <input type=”email” required>

    Maybe Apple knows something we don’t!

    They like one character top-level domains names.

  9. You can use parseFloat() instead of parseInt() in the accelerometer demo to smooth the movement. I first thought the accelerometer data was too crude, but now it moves nicely.

Leave a Reply

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