views:

83

answers:

2

is there a script to detect, if the visitor use iphone (whatever it's browser, may iphone Safari, iPhone for Opera or etc.)?

Then will shutdown some some of my JavaScript.

Thanks...

A: 
<script language="javascript" type="text/javascript"> 
    //This redirects iPhone users to the iPhone-friendly site
    if ((navigator.userAgent.indexOf('iPhone') != -1) ||
    (navigator.userAgent.indexOf('iPod') != -1)) {
        document.location = "http://i.yoursite.com";
    }

This script checks for iPhone or iPod in the userAgent and then executes an action. Give this a try.

d2burke
ok ill test this script. hope it's work :)
GusDe CooL
Ok, let me know how it goes for you. I've used it several times and seems to pick up find. The next step is adding other strings and then adding actions for each of them depending on the functionality you want.
d2burke
+2  A: 

searching on the net there are two common ways of achieving this. My favorite though is in PHP its just so clean? wow. :D

and in PHP you say

<?php

function isIphone($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'iPhone') !== FALSE);
}

if(isIphone()) {
    header('Location: http://www.yourwebsite.com/phone');
    exit();
}

// ...THE REST OF YOUR CODE HERE

?>

and in javascript you say

var agent = navigator.userAgent;
var isIphone = ((agent.indexOf('iPhone') != -1) || (agent.userAgent.indexOf('iPod') != -1)) ;
if (isIphone) {
    window.location.href = 'http://www.yourwebsite.com/phone';
}

Hope that helps.

PK

Pavan
Great... thanks you very much give the code in PHP Script. seems it's the best solution, since i want do different task for different browser which i want it to be processed from the server :D
GusDe CooL
i know exactly what you mean. I love php. When a user right click source you cant see any of the code used to program the site. Its very flexible as well. and its easy to use. All the code is processed on the server which is a bonus. Hope this helps.if you need any more help... do let me know. Tweaking of this code can be done if any is needed to be done.
Pavan
infact you can enhance this code to also check if it is an ipod. if you need help with that do let me know.Also while you're here mark this post as answered thanks. so people know that this is the correct answer and they can then use this as a reference as youve tried it out. thank you
Pavan
yes, i will mark this as correct answer as soon i test this in iPhone environment :D, now i still download the virtual software to test it's :)... Thank you very much :D
GusDe CooL
Hello i already test this with my Tester it's work, now i need for Ipad too. can you help me with the code?
GusDe CooL
Hi for ipad detection is very simple. you can either the word iPhone' in the php code with 'iPad' and that will work. Another way you can do it which i found out today is by using one line which will store a boolean value in a variable to say whether or not there is a device being detected based on the name you give, (iPhone, iPod, iPad):$isiPad = (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
Pavan