Why is browser sniffing not a recommended practice?

前端 未结 7 1784
旧时难觅i
旧时难觅i 2020-12-01 16:26

You hear it all over the place: using javascript to sniff the user agent string to detect browser versions is a Very Bad Thing. The latest version of jQuery has now deprecat

7条回答
  •  猫巷女王i
    2020-12-01 16:54

    As well as the issues about browser-sniffing being inferior to capability-sniffing, handling navigator.userAgent as a string is in itself a very unreliable way of browser-sniffing.

    It might work better if every browser stuck to the “Name/version” scheme of identifying themselves, but they don't. Most browsers claim to be “Mozilla/some.version” regardless of what they are. And that bit at the start is the only readily parsable part of the string; the rest is completely unstandardised. So scripts started searching the whole string for characterists substrings like “MSIE”. This is a disaster.

    • Some browsers deliberately spoof each other, including substrings like “MSIE”, “Gecko” and “Safari” in their user agent strings when they're not those browsers, mostly to defeat ill-conceived string sniffers.

    • Some browsers allow the entire user agent string to be spoofed under user control.

    • Some browser variants aren't. For example IE Mobile is nothing at all like regular IE, but “MSIE” will still match it.

    • Some browsers allow add-ons to write extra tokens to the user agent string including arbitrary text. Just one registry change by a rogue add-on can make MSIE look like Firefox.

    • String matching is just inherently unreliable. For example a new browser called “CLUMSIERbrowser” would match MSIE.

提交回复
热议问题