• Brian Boorman (google)

    Isn't it possible that this is just stale code that is still there because that portion of the code base didn't need to be touched in a whole bunch of years? As soon as you start mucking with things that aren't broken you risk introducing new bugs. Hardly makes this WTF worthy.

  • (nodebb)

    Return the version number, or return false if it's not Internet Explorer.

    This is the WTF. There are two pieces of information to return. One is conditional depending on the other, but the function fuses them into a single return of just one value, rather than returning a tuple or similar, either (Python-ish syntax for the tuple, sorry):

       return (false,);  # not IE
    

    or

       return (true, version_number); # is IE, version is version_number
    
  • Hanzito (unregistered)

    The solution is of course return 12 instead of false.

  • TS (unregistered)

    "there's simply no excuse for this user-agent sniffing game."

    Yep, just test in Chrome, and if it works, job done.

  • ND (unregistered)

    Ah, that old chestnut. But really, we should be asking ourselves... What Is Truth?

    if (isIE() != 'FileNotFound' && isIE() <= 10) { alert("The browser you are using is too old and not supported anymore. Please get a newer one."); }

  • Sole Purpose Of Visit (unregistered) in reply to Hanzito

    No no no. Return (uint.Max / 2) - 1234, or equivalent.

    This guards against almost all future agent numbering systems, avoids the possibility that some loon compiler has coalesced the range of unsigned integers with the range of signed integers, and also guards against the possibility of the agent string being defaulted to uint.Max.

    I mean, obviously, there's a small keyhole propensity to error here, but I would claim that it is unlikely.

  • Sole Purpose Of Visit (unregistered)

    The other WTF thing here is the fact that ECMA5 (AKA Javasript in 2009, which I devoutly hope is the minimum here) coerces a boolean into an integer.

    I realise that Javascript was knocked together in about two weeks, and frankly it was a phenomenal achievement. But if you're going to create a language that leans heavily towards FP, you really don't want to rely on crusty ole C style coercions.

  • (nodebb)

    Jokes aside, I would have gone with a function name of "GetIEVersion" and it can return null, if not IE, or a number, if it is. Then you can do:

    if(GetIEVersion() !== null && GetIEVersion() <= 10) { ... }

  • (nodebb)

    : 0; // Fixed

  • Rob (unregistered)

    JavaScript isn't the only language that shows this behaviour. PHP, a language with a library set notorious for its many, many "return a proper value or otherwise false" functions, also regards false as <= 10.

  • Yikes (unregistered)

    Classic IE exceptionalism. It looks like when they finally began to comply with RFC7231 5.5.3 in IE11, "msie" was replaced with Trident, so if (isIE() && isIE() <= 10) in particular could very well be replaced with just if (isIE()) .

  • Kleyguerth (github)

    There's still a valid case for user agent sniffing... When you want to show instructions on how to "install" a PWA. Each browser does their own thing, you either sniff and give the user the correct instructions or you list them all and let the user figure it out (the user won't).

  • tbo (unregistered) in reply to Steve_The_Cynic

    Or you could argue there's one piece of information to return: the IE version number. And if there's no version number, you shouldn't return a number. So return something else. Nowadays, that's null, but in a lot of cases, false is semantically equivalent.

    I would argue that the WTF is that a function named is* returns anything other than true or false. If it were named something like getIEVersion(), it's a perfectly fine function, though nowadays we probably would return null instead of false.

    I think people tend to forget that in the environment for which JavaScript and PHP were developed — handling HTML form input fields — ALL data starts as a string, and enforcing a strict type structure in such an environment just adds tedium.

  • Loren Pechtel (unregistered)

    Much simpler approach: Return -1 for the version if it's not there. Version numbers are always positive.

  • a cow (not a robot) (unregistered) in reply to Loren Pechtel

    Wouldn't work properly: it would detect Firefox as a "too old Explorer".

  • Scragar (unregistered)

    Makes more sense to just take a test version as an argument and return true if the version matches or false otherwise.

     if (isIE(10)) return alert("upgrade your browser already")
    
  • irishgirl (unregistered)
    Comment held for moderation.
  • apkmods.games (unregistered)
    Comment held for moderation.
  • Tim (unregistered)

    As others have alluded, the issue is not really about JavaScript's type conversion; it's with trying to return 2 things from the same function.

    In a strongly typed language you would have probably chosen to return to return either 0 or -1 if the browser was not IE (or if you were a real pedant you could return a tuple or a union type) but whatever you did and would have led to very similar logic required of the caller, because you need to make 2 separate tests of the result

  • (nodebb) in reply to Sole Purpose Of Visit

    It's a bit unfair to criticise javascript for doing exactly the same thing as C, C++, .... do

    there are times when I feel fortran is an improvement on C because it doesn't let you do stupid things like that.

  • Fworg64 (unregistered) in reply to Loren Pechtel

    Challenge accepted

  • farhankanju (unregistered)
    Comment held for moderation.
  • ProVstPc (unregistered)
    Comment held for moderation.
  • Software House (unregistered)
    Comment held for moderation.

Leave a comment on “A Sniff”

Log In or post as a guest

Replying to comment #570532:

« Return to Article