Stop testing for indexOf('iPhone'), while there's still time.

21 views
Skip to first unread message

Richard Herrera

unread,
Sep 7, 2007, 12:56:30 PM9/7/07
to iPhoneWebDev
A number of iPhone apps are using JS to sniff iPhone and serve content
based on that. While I haven't seen the user agent string for the
upcoming Touch, chances are likely it will not contain
indexOf('iPhone'). This means it only takes one viewing for that new
iPod Touch user to declare that your app suxxors, et al.

Webkit.org has released a very nice detection script that is
compatible with both devices:
http://webkit.org/blog/119/webkit-detect-script-updated-for-iphone-and-ipod-touch/

But me being the ever-perfectionist iPhone developer, I've stripped
out what's not needed if all you need is i(Phone || Touch) detection:

var WebKitDetect = {
isWebKit : function() {
return new RegExp(" AppleWebKit/").test(navigator.userAgent);
},
isMobile : function() {
return WebKitDetect.isWebKit() && new RegExp("
Mobile/").test(navigator.userAgent);
},
mobileDevice : function() {
if (!WebKitDetect.isMobile()) {
return null;
}
var fields = new RegExp("(Mozilla/5.0 \\()([^;]
+)").exec(navigator.userAgent);
if (!fields || fields.length < 3) {
return null;
}
return fields[2];
}
};


Usage:

if (WebKitDetect.mobileDevice()) {
setInterval(updateLayout, 400);
}

nroose

unread,
Sep 10, 2007, 7:40:08 PM9/10/07
to iPhoneWebDev
Well, that's clear and concise! Even your stripped version is huge
compared to just looking for "iPhone". Can you tell us what the
browser sig will be for the touch? Seems like if we just search for
iPod or iPhone we should be OK, no? At least until there are a
plethora of different ones, or perhaps a desktop iPod...

On Sep 7, 9:56 am, Richard Herrera <docty...@gmail.com> wrote:
> A number of iPhone apps are using JS to sniff iPhone and serve content
> based on that. While I haven't seen the user agent string for the

> upcomingTouch, chances are likely it will not contain
> indexOf('iPhone'). This means it only takes one viewing for that newiPodTouchuser to declare that your app suxxors, et al.


>
> Webkit.org has released a very nice detection script that is

> compatible with both devices:http://webkit.org/blog/119/webkit-detect-script-updated-for-iphone-an...

Reply all
Reply to author
Forward
0 new messages