With all the buzz about the iPad, I wanted to post another useful snippet of code. It detects whether the person viewing your website is on a iPad. If they are on a iPad, you can redirect them to a iPad version website or just to another page, its up to you. If they are on a normal computer it doesn’t redirect them and they go on as normal. Its just some simple javascript, and if you want to use it just change the “document.location” value to the url you want them redirected. Here is the code, and its just the same as when you detect for the iPhone with an extra argument.
1 2 3 4 5 | <script type="text/javascript"> // <![CDATA[ if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1)) { document.location = "http://www.scottrockers.com/iphone.html"; } // ]]> </script> |
Cya
Tags: Detection, Development, iPad, javascript









Should the line containing “navigator.userAgent.indexOf(‘iPod’)” read “navigator.userAgent.indexOf(‘iPad’)” ???
iPad not iPod as it were.
Rod
Hey Rod, The iPad redirection code is the exact same as a iphone or iPod redirection code, but it has another argument added to it that looks for “iPad” in the userAgent of the browser as well. The extra argument in the if statement looks like this “|| (navigator.userAgent.indexOf(‘iPad’) != -1)”
Update: This Code works great, thanks Scott.
This code does not work on the iPad. I just tested redirect with my website http://www.briankellogg.com in the Apple store and it does not work. The code still works for the iPhone. If someone does figure out a better code please post it here.
Hey Brian,
I went on a search to check to make sure this script was working and found out some things. The userAgent of the iPad is: “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″
So the code should be working because it contains the words iPad in it. If anyone can actually test this out on the actual iPad, and not in the Apple Store, then please let me know and I will post a correction. Brian, I did update the code just to make sure, so please try it again.
Thanks,
Scott
I tested this script on an ipad and worked smoothly.
Scenerio: I have a website for web(PC) and a redirect for mobile: Iphones, blackberries etc. I found my redirect for iphone and ipod touch was also redirecting for the ipad. I did NOT want IPAD users to be redirected so I used the above script to detect ipad users and NOT redirect to the mobile site. It works well.
The script above is indeed good!.
Thanks,
David Morgan
Web Developer
Thanks Dave, much appreciated.
Hi Scott,
The updated code works great. I just tested it on the iPad. Thanks again for helping me out.
Thanks for this code! Is it possible to redirect ipods/iphones to one site while redirecting the ipad to another site? Thanks in advance…
Hey Jason,
Here is the code for just ipods and iphones.
And here is just for the ipad.
Scott
Is there a way to sneak in a blackberry redirect to the same script?
Thanks by the way. This works perfectly on ipod, ipad and iphone for me.
I looked up the userAgent of the BlackBerry, so I think you can use this code. Could you test it out and let me know and leave a reply on this comment that would be great.
From what I can tell, the script for the Blackberry does not redirect.
I rechecked the useragent and it should be right. I changed the blackberry to BlackBerry, could someone check my changes and see if this script works for the blackberry.
Thanks
Scott
Great script, thanks! Side question: do you know how, on the iphone, to prevent an embedded QT (.mp4) file from automatically opening up full screen and, instead, playing within the webpage itself?
Not sure Michael, sorry.
Michael, have you tried using the HTML5 method? This may work.
Hi there, i tried the blackberry code and it works perfect !!! it´s definetely a good tip!! i just was wondering, if i want to have a web version for iphone, another one for blackberry, etc … do i have to copy in a row both textis (for example first the code for the iphone recognition followed by the blackberry code??????) thanks a lot.
the script for blackberry works ??
Thank you for these scripts. very helpful.
I think so, Estaban tried it and he said it works. Please let me know if it doesn’t.
Thanks
Scott
worked like a charm for both the iPad and iPhone. thank you!
I was just testing the BlackBerry script and could not get it to work.
Although I said it doesn’t work, does not mean that the code is incorrect. I just realized that the phone I tested with does not have JavaScript enabled, so it obviously would not redirect. Sorry about that ADMIN! The script does look good and does work for iphone/pad.