iPad web development tips
By now you, or someone you know, has an iPad. Of course, Apple has been preparing people on how to design their web sites for optimal use with the iPad (see Preparing your Web Content for iPad). Of course, the biggest thing is the lack of Flash support on the iPad. Apple is instead pushing people to use “HTML5.” I put that in quotation marks because most of the contexts in which I’ve seen this have been confusing. What Apple appears to mean by “HTML5″ is that they want you to use <video> instead of Flash video and their CSS animations and transitions instead of Flash effects.
Semantics aside, there’s a lot of information about how your site can be made to best work with the iPad, and I’d like to add a few nuggets to that body of knowledge.
User-agent string
The previously-linked post describes the iPad Safari user-agent to be in the following format:
Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10
This was the user-agent string during the beta testing phase. The format is slightly different for the final release: Although this appears to be the official user-agent string, I have received reports of a user-agent like this:
Mozilla/5.0(iPad; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B314 Safari/531.21.10
You’ll note the addition of “iPhone” in the operating system segment of the user-agent string. This brings the user-agent string for Safari on the iPad more into line with Safari on the iPhone and iPod Touch, which have the following user-agent strings, respectively:
Mozilla/5.0 (iPod; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16
Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16
This means a single user-agent string sniff for “iPhone” returns true in all three cases. The problem, of course, is that you might want to serve different experiences to the iPhone/iPod Touch than you would for the iPad. Make sure you double-check any user-agent sniffing designed to target these devices.
JavaScript iPad detection
If you’re trying to detect the iPad using JavaScript, there’s a very simple way to do so: check navigator.platform. The value of navigator.platform is always “iPad” when Safari for iPad is the user-agent. This follows the tradition of “iPhone” for the iPhone and “iPod” for the iPod Touch. This is the most accurate way to detect the iPad from JavaScript, assuming you don’t want to do a full user-agent string sniff.
function isIPad(){
return navigator.platform == "iPad";
}
Also, keep in mind that navigator.platform doesn’t change even when the user-agent string for a browser is changed.
Screen dimensions and orientation
The iPad documentation says that the screen resolution is 1024×768 as opposed to 480×320 for the iPhone/iPod Touch. You can detect screen resolution via JavaScript by using screen.width and screen.height, though it may not work quite as you expect on the iPad. One would think that screen.width would be 768 when being held vertically and would be 1024 when held horizontally. However, screen.width is always 768 regardless of the way you’re holding the iPad; likewise, screen.height is always 1024. It’s worth noting that this is the same behavior as on the iPhone and iPod Touch.
Safari on the iPad does support the window.orientation property, so if necessary, you can use that to determine if the user is in horizontal or vertical mode. As reminder of this functionality:
window.orientationis 0 when being held verticallywindow.orientationis 90 when rotated 90 degrees to the left (horizontal)window.orientationis -90 when rotated 90 degrees to the right (horizontal)
There is also the orientationchange event that fires on the window object when the device is rotated.
You can also use CSS media queries to determine if the iPad is being held in vertical or horizontal orientation, such as:
<link rel="stylesheet" media="all and (orientation:portrait)" href="portrait.css">
<link rel="stylesheet" media="all and (orientation:landscape)" href="landscape.css">
In this way, you can target styles specifically at different orientations. For more information, please see iPad Orientation CSS.
Mobile Safari is mobile Safari
The good news for web developers is that there’s not a lot of difference between Safari on the iPad versus Safari on the iPhone or iPod Touch. Though the user-agent string is different and the screen size is different, the core browser remains the same. Thus, all of the capabilities that you’ve grown to learn about, such as touch and gesture events, are all there for the iPad. I cover these events, along with other mobile Safari-specific functionality, in Professional JavaScript for Web Developers, 2nd Edition.
Update (8-April-2010): Added references to iPad orientation CSS.
Update (21-April-2010): Updated description of user-agent strings.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.




13 Comments
The user-agent string that I’m seeing on my iPad doesn’t have ‘iPhone’ in it. It’s still showing up as the first one you mentioned, “Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us)”. Are you seeing something different? I can’t imagine it’d be something related to the model (I have a 16 GB one).
Ernie Turner on April 6th, 2010 at 2:40 pm
Cache limits was a big concern for Mobile Safari on the iPhone. Do you know what sort of limits we’re looking at here?
Paul Irish on April 6th, 2010 at 5:38 pm
i read that one difference between Mobile Safari for iPad and iPhone was that Webkit honors CSS media queries based on orientation for iPad while not for iPhone. are you aware of this and/or can you confirm?
basically you just add two stylesheets, one for portrait and one for landscape. it seems like the easy way, which usually is the worst way. i foresee tons of duplicate content and/or duplicate id’s on pages using this technique.
J. Albert Bowden II on April 6th, 2010 at 6:31 pm
@Ernie – I’ve heard now from people who’ve seen both. I wonder what’s causing this discrepancy. Hmm.
@Paul – I’ve not dug into the cache issues, very good idea, though.
@Albert – I’ve not heard of that. I suppose it would make sense from a designer’s point of view, but it seems inconsistent with the behavior of screen.width.
Nicholas C. Zakas on April 6th, 2010 at 10:13 pm
To followup on Albert’s point, the iPad does support media queries to detect orientation. I’ve added that reference.
Nicholas C. Zakas on April 8th, 2010 at 8:05 pm
[...] with the large screen on the iPad. Here, some things to keep in mind for iPad-friendly web design.iPad web development tipsSemantics aside, there’s a lot of information about how your site can be made to best work with [...]
Useful Design Tips For Your iPad App - Smashing Magazine on April 16th, 2010 at 12:15 pm
[...] read some info about this and conclude, that some iPad user agent strings include the word “iPhone” [...]
iPad detection the right way | Barklund.org on April 23rd, 2010 at 9:00 am
Wow, the JavaScript iPad detection code was exactly what i was looking for, for my current project!
Desi Webb on May 11th, 2010 at 6:44 am
This post has saved me tons of time. I’m developing an iPad website for our company and this is an excellent resource for me to have. Thank you for your work. Please continue to post more!
– David
David on May 27th, 2010 at 4:10 pm
[...] and Orientation in CSS – A slightly more in-depth look at how you can target the iPad using CSS.iPad Web Development Tips – Working through a number of different CSS/JS techniques, and explaining why the browser really [...]
The iPad Web Design & Development Toolbox | Best Web Magazine on June 24th, 2010 at 8:01 am
Thanks, Nicholas.
I’ve been looking for the screen orientation script.
Cheers.
Amir Khella on July 15th, 2010 at 3:30 am
[...] iPad web development tips (Nicholas C. Zakas) iPhone window.onorientationchange Code (Ajaxian) The orientation media query (Quirksmode) [...]
Dealing with device orientation (Mobile web part 6) | David B. Calhoun – Developer Blog on August 24th, 2010 at 4:42 am
FYI, the above link for “Preparing Your Web Content for iPad” is now outdated. The current link is http://developer.apple.com/library/safari/#technotes/tn2010/tn2262/.
Steve Kellman on December 27th, 2010 at 12:05 pm
Comments are automatically closed after 14 days.