How to Detect iPad and Redirect to iPad Version Website

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: , , ,

23 Responses to “How to Detect iPad and Redirect to iPad Version Website”

  1. Rod Pascoe says:

    Should the line containing “navigator.userAgent.indexOf(‘iPod’)” read “navigator.userAgent.indexOf(‘iPad’)” ???

    iPad not iPod as it were.

    Rod

  2. admin says:

    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)”

  3. Brian says:

    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.

  4. admin says:

    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

  5. Dave says:

    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

  6. admin says:

    Thanks Dave, much appreciated.

  7. Hi Scott,
    The updated code works great. I just tested it on the iPad. Thanks again for helping me out.

  8. Jason says:

    Thanks for this code! Is it possible to redirect ipods/iphones to one site while redirecting the ipad to another site? Thanks in advance…

  9. admin says:

    Hey Jason,

    Here is the code for just ipods and iphones.

    1
    2
    3
    4
    5
    
    <script type="text/javascript">
    	if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1)) {
    		document.location = "http://www.scottrockers.com/iphone.html";
    	}
    </script>

    And here is just for the ipad.

    1
    2
    3
    4
    5
    
    <script type="text/javascript">
    	if ( (navigator.userAgent.indexOf('iPad') != -1)) {
    		document.location = "http://www.scottrockers.com/iphone.html";
    	}
    </script>

    Scott

  10. Stipko says:

    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.

  11. admin says:

    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.

    1
    2
    3
    4
    5
    
    <script type="text/javascript">
    	if ((navigator.userAgent.indexOf('BlackBerry') != -1)  ) {
    		document.location = "http://www.yourwebsitename.com/yourwebpagename.html";
    	}
    </script>
  12. Sarah says:

    From what I can tell, the script for the Blackberry does not redirect.

  13. admin says:

    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

  14. MIchael says:

    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?

  15. admin says:

    Not sure Michael, sorry.

  16. Jon says:

    Michael, have you tried using the HTML5 method? This may work.

  17. Esteban says:

    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.

  18. sofiane says:

    the script for blackberry works ??

  19. crumpy says:

    Thank you for these scripts. very helpful.

  20. admin says:

    I think so, Estaban tried it and he said it works. Please let me know if it doesn’t.

    Thanks
    Scott

  21. amelia says:

    worked like a charm for both the iPad and iPhone. thank you!

  22. James says:

    I was just testing the BlackBerry script and could not get it to work.

  23. James says:

    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.

Leave a Reply