I am playing around with some browser detection code – I hasten to add this is for logging purposes, not to deliver browser targeted web content. I personally have an interest in only a relativley small number of browsers – Firefox, IE & Opera.
I guess I am almost bound to upset someone by being dismissive of other browsers but my experience with my site is that these three account for than 99% of the traffic.
Nevertheless, I thought it would be useful to get some feedback on how it works with other browsers. I would be much obliged to anyone who cares to copy the code below into an HTML file, open it in any browsers you have handy and report back on the number in the alert box.
[CODE]<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<script>
function LSF(AFlag,AShift){return (AFlag)?1 << AShift:0}//LeftShiftFlag
function agentInfo()
{
var ua = navigator.userAgent.toLowerCase();
var e = document.getElementById(‘main’);
var i = LSF(e.textContent != null,0) + LSF(window.opera != null,1) +
LSF(e.innerText != null,2) + LSF(document.all != null,3);
alert(i);
}
</script>
</head>
<body onload=”agentInfo()”>
<div id=”main”>Empty</div>
/*Using the id of main for the body element caused problems in Opera*/
</body>
</html>
The logic I am using is this
[LIST][*]
Firefox understands textContent and nothing else. Returns 1
Opera understands textContent, innerText, document.all and also supports window.opera Returns 15
MSIE understands innerText and document.all. Returns 12
Safari (Windows beta) understands textContent and innerText. Returns 5
I gather that Konqueror reliably returns navigator.vendor = ‘kde’ but I have no way of verifying this. Thanks in advance for all your feedback.
p.s. – I should add that I only really care about relatively recent browsers. Once again, in my experience the number of people using older versions is too small to be important. I guess I will upset some purists again :-/