Detect iPad using Javascript

I’ve got some people asking for a simple Javascript-Detection for iPad’s.

It’s simple, just take a look at the browser identification:

 

Example:


It basically searches in the browser information for any occurence of “iPad” and returns true if found.

Here’s an example of Safari’s identification on the iPad:

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/4.0.5 Mobile/7B367 Safari/6531.22.7

Detecting iPad Orientation in Safari using JavaScript

I stumbled on that by accident. One of my javascript-variables was called orientation and was not correctly used on the iPad’s Safari.

After looking at its content, I found out that you can detect the device orientation. I knew you can handle many cool things on the iOS, like swipes in javascript, but this one was new to me.

So, what are you going to do with this? It’s obvious: Style your website or re-order your content to match exactly your iPad’s orientation ;-)

You can use javascript in an event handler, loop, or whatever by accessing window.orientation (or only orientation).

Here’s an example on how to detect the current orientation of the iPad device either by pressing a button or when the orientation changes, using an event called onOrientationChange:





You can also use CSS Stylesheets using the media definition:




The detection is not restricted to the iPad, it should work on all iOS devices.

Switch to our mobile site