Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  175] [ 4]  / answers: 1 / hits: 81796  / 13 Years ago, sat, may 28, 2011, 12:00:00

Most implementations i've seen are for browser detection on the client side. I was just wondering if it was possible to do browser detection before sending any resources to the client.



Thanks.


More From » browser

 Answers
7
var ua = request.headers['user-agent'],
$ = {};

if (/mobile/i.test(ua))
$.Mobile = true;

if (/like Mac OS X/.test(ua)) {
$.iOS = /CPU( iPhone)? OS ([0-9._]+) like Mac OS X/.exec(ua)[2].replace(/_/g, '.');
$.iPhone = /iPhone/.test(ua);
$.iPad = /iPad/.test(ua);
}

if (/Android/.test(ua))
$.Android = /Android ([0-9.]+)[);]/.exec(ua)[1];

if (/webOS//.test(ua))
$.webOS = /webOS/([0-9.]+)[);]/.exec(ua)[1];

if (/(Intel|PPC) Mac OS X/.test(ua))
$.Mac = /(Intel|PPC) Mac OS X ?([0-9._]*)[);]/.exec(ua)[2].replace(/_/g, '.') || true;

if (/Windows NT/.test(ua))
$.Windows = /Windows NT ([0-9._]+)[);]/.exec(ua)[1];


That should work for you. Just put it in your response handler.


[#92003] Thursday, May 26, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jameson

Total Points: 534
Total Questions: 103
Total Answers: 102

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
jameson questions
;