/    Sign up×
Community /Pin to ProfileBookmark

You could help

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>
[/CODE]

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


  • [/LIST]

    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 :-/

    to post a comment
    JavaScript

    8 Comments(s)

    Copy linkTweet thisAlerts:
    @KorNov 06.2007 — Use DOM methods

    standard:

    http://www.w3.org/DOM/

    tutorials:

    http://www.w3schools.com/dom/default.asp

    http://www.howtocreate.co.uk/tutorials/javascript/dombasics
    <i>
    </i>var e = document.getElementById('main');
    var i=e.firstChild.nodeValue;
    alert(i);


    You may also use the non-standard [B]innerHTML[/B] method. It is crossbrowser, so far, but it depends on which is your final aim.
    Copy linkTweet thisAlerts:
    @FredAtauthorNov 06.2007 — Not sure what that reply has to do with my post.
    Copy linkTweet thisAlerts:
    @KorNov 06.2007 — well, yes there are some differences between browsers, but not on your example. For instance, there are different methods to events attachment, events capture, childNodes number... a.s.o. ..., not too may, but important ones, most of the time.

    But, speaking of detector, I advice you to use rather a method/object detector rather than a [I]navigator[/I] one

    The reason is double.

    One: In the navigator properties some browsers mime to be otherwise (Opera->IE), Netscape->Moz...)

    Two: Some methods works in IE only, some works in IE and Opera, some works in Moz and Opera only, some in IE and Moz only...

    Conclusion: never use a [I]browser[/I] detector. Use a [I]method/object[/I] detector (if(method){}) when there is no other crossbrowser method
    Copy linkTweet thisAlerts:
    @KorNov 06.2007 — Not sure what that reply has to do with my post.[/QUOTE]
    It looks like you are not connected with the modern HTML/CSS/javascript changes.
    Copy linkTweet thisAlerts:
    @KorNov 06.2007 — ... Besides all you have not specified the type of your script
    Copy linkTweet thisAlerts:
    @KorNov 06.2007 — Have a look in IE, Moz, Opera and what else you want:
    <i>
    </i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
    &lt;html&gt;
    &lt;head&gt;
    &lt;script type="text/javascript"&gt;
    function agentInfo(){
    var e = document.getElementById('main');
    var i = e.firstChild.nodeValue;
    alert(i)
    }
    onload=function(){agentInfo()}
    &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
    &lt;div id="main"&gt;Empty&lt;/div&gt;
    &lt;/body&gt;
    &lt;/html&gt;
    Copy linkTweet thisAlerts:
    @FredAtauthorNov 07.2007 — If you can't be bothered reading through a post you shouldn't bother writing half baked replies either. If you had read my post you would have understood what I am after - and also that I am not in fact using navigator at all. As to specifying the script type - this was intended to be a short piece of test code not a masterclass in standards compliance.
    Copy linkTweet thisAlerts:
    @KorNov 08.2007 — If you can't be bothered reading through a post you shouldn't bother writing half baked replies either. If you had read my post you would have understood what I am after - and also that I am not in fact using navigator at all. As to specifying the script type - this was intended to be a short piece of test code not a masterclass in standards compliance.[/QUOTE]
    I am used to read [I]a[/I] post, not [I]through[/I] a post. If you do like riddles, than you should choose another Forum. As a Moderator I have read and answered thousands of posts so far. All the time I try to give solutions. I thought you try to find a crossbrowser solution to a problem. I have given you one. That is all.?
    ×

    Success!

    Help @FredAt spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.27,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...