views:

48

answers:

2

Hi guys.

Quick question. I am writing a website and would like to redirect users to different webpages depending on their browser and also their version of the iPhone operating software. I know you may come back at me and say it should be up to the user to decide which site they view, they don't have a choice in this matter.

The code I am using is in javascript and can identify between the different browsers but not the iPhone operating systems which is the key to this. This is the code:


if(navigator.userAgent.match(/iPhone/i)) {
    if(navigator.userAgent.match(Version/4)){
        if(document.cookie.indexOf("iphone_redirect=false") == -1) window.location ="iPhone4.html";
    }
    if(document.cookie.indexOf("iphone_redirect=false") == -1) window.location ="iPhone.html";
}
//Rest of the site goes here

Not sure if the brackets are right here it's early and may have got that wrong but any help will be appreciated.

Sam

+1  A: 

The iPhone 4 user agent string is setup like this:

Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7

Should help you the old iPhone is:

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

So in your code, the version would be 4.0.5 for the iPhone 4 and 4.0 for the iPhone.

Brad F Jacobs
Thanks for the reply, the problem with the code is that it wont select the correct page when running iOS4 meaning the second "if" statements isn't parsing correctly, at the moment I have the code:'if(navigator.userAgent.match(/iPhone OS 4/i)){'This is supposed to detect whether the iPhone is running iOS4.
SamRowley
Mind posting the full code you are using, I do not see that portion in the original question, it will help out a bit to help you get what you need.
Brad F Jacobs
+1  A: 

This should work:

if(navigator.userAgent.match(/iPhone/i)) {
    if(navigator.userAgent.match(/iPhone OS 4/i)) {
        if(document.cookie.indexOf("iphone_redirect=false") == -1) {
            window.location ="iPhone4.html";
        }
    }
    else {
        if(document.cookie.indexOf("iphone_redirect=false") == -1) {
            window.location ="iPhone.html";
         }
    }
}
Shaji