So far, I am doing browser details detection, at client side by using JavaScript.
data['browserDetails'] = {
'browser' : getBrowser(),
'majorVersion' : getBrowserMajorVersion(),
'appCodeName' : navigator.appCodeName,
'appName' : navigator.appName,
'appVersion' : navigator.appVersion,
'language' : navigator.language,
'platform' : navigator.platform,
'cookieEnabled' : navigator.cookieEnabled,
};
// http://stackoverflow.com/a/9851769
function getBrowser()
{
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
if (isOpera) {
return 'Opera';
}
var isFirefox = typeof InstallTrigger !== 'undefined';
if (isFirefox) {
return 'Firefox';
}
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
if (isSafari) {
return 'Safari';
}
var isIE = /*@cc_on!@*/false || !!document.documentMode;
if (isIE) {
return 'IE';
}
var isEdge = !isIE && !!window.StyleMedia;
if (isEdge) {
return 'Edge';
}
var isChrome = !!window.chrome && !!window.chrome.webstore;
if (isChrome) {
return 'Chrome';
}
return 'Unknown';
}
// http://stackoverflow.com/a/38080051
function getBrowserMajorVersion()
{
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return (tem[1] || '');
}
if (M[1]=== 'Chrome') {
tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
if (tem!= null) return tem[2];
}
M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if ((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M[1];
}
I had once try to perform browser details detection with server side code, by using PHP + WURFL. With my very limited test cases, it doesn't show server side detection is any better (accuracy) than client side detection.
I was wondering, what is the best practice for browser details detection? As far as accuracy is concerned, should I do it in client side (Using JavaScript) or server side (Using PHP and WURFL)?
Thanks.