Just recently wrote about the difference between Device Detection, User-Agent sniffing and Feature detection. A bad example of UA sniffing is the new New York Times mobile site. See some sample code below. Not very elegant, scalable, future friendly or maintainable. Even if the newspaper is a “simple” site, with few bells and whistles, this snippet will most likely grow to become impossible to maintain, or it will simply not be touched after launch. Looks like in this specific case, Device Detection would be a much better solution to their problem.
var ua = navigator.userAgent;
window.BBDevice = {
isOldBB: false,
indexOfVersion: ua.indexOf("Version/"),
indexOfBB: ua.indexOf("BlackBerry"),
fullVersion: null,
version: null
};
if (window.BBDevice.indexOfBB >= 0) {
existingClasses = document.body.className = existingClasses + " bb";
if (ua.indexOf("WebKit") < 0) {
existingClasses = document.body.className = existingClasses + " oldbb";
window.BBDevice.isOldBB = true;
window.BBDevice.fullVersion = ua.substr(window.BBDevice.indexOfBB);
window.BBDevice.fullVersion = window.BBDevice.fullVersion.substr(window.BBDevice.fullVersion.indexOf("/") + 1, window.BBDevice.fullVersion.indexOf(" "));
window.BBDevice.version = window.BBDevice.fullVersion.substr(0, window.BBDevice.fullVersion.indexOf("."));
} else {
if (window.BBDevice.indexOfVersion >= 0) {
window.BBDevice.indexOfVersion = window.BBDevice.indexOfVersion + 8;
window.BBDevice.fullVersion = ua.substr(window.BBDevice.indexOfVersion);
window.BBDevice.fullVersion = window.BBDevice.fullVersion.substr(0, window.BBDevice.fullVersion.indexOf(" "));
window.BBDevice.version = window.BBDevice.fullVersion.substr(0, window.BBDevice.fullVersion.indexOf("."));
}
}
} else if (ua.indexOf("MSIE 9.0") >= 0 || ua.indexOf("IEMobile/9.0") >= 0) {
existingClasses = document.body.className = existingClasses + " win75";
}