How to detect browser rendering engine in javascript?

后端 未结 3 738
说谎
说谎 2021-01-12 16:25

I read a book calls \'Professional Javascript For Web Developers 2nd edition\' and it state that this code can detect browser rendering engine:

    

        
3条回答
  •  既然无缘
    2021-01-12 17:16

    The script obviously doesn't work - it just sets a bunch of variables to zero or null and then checks if they're zero or null. My guess would be that this is not the whole script, and that the crucial part is missing.

    What one would normally do is read the UserAgent string (I'm too lazy to google how exactly you do that) and throw some regular expressions at it to detect known patterns. This kind of user agent sniffing has a bunch of downsides though:

    1. It's not forward compatible. You don't know which UA strings future browsers are going to use, so most likely, newer browsers will fail your test and receive the most stripped-down version, even though they could easily handle the full-eye-candy-blow-your-mind one.

    2. The UA string isn't reliable. Just because so many websites make the mistake of sniffing, and then not getting updated regularly, users set different UA strings, purposefully reporting a "wrong" render engine. And then they forget to switch it back, causing all sorts of weird behaviour.

    3. Just because someone has a certain render engine doesn't mean you can rely on all features being there. Browsers are highly configurable, and people use all sorts of extensions, blocking things selectively or completely.

    Long story short, instead of sniffing the UA string and assuming things are what you think they are, just test features individually and directly.

提交回复
热议问题