Tutorial :Javascript iPhone selection


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.



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";           }      }  }  


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »