How to target Edge browser with javascript

后端 未结 9 744
無奈伤痛
無奈伤痛 2020-12-05 09:26

I know you should do feature detection where possible, but can you detect in Javascript if the browser is the Microsoft Edge browser?

I maintain an old product and I

9条回答
  •  难免孤独
    2020-12-05 10:11

    Try to detect features instead of a specific browser. It's more future-proof. Only rarely should you use browser detection.

    With that out of the way: one option is to use a library (there are many intricacies to User Agent strings), or alternatively to parse window.navigator.userAgent manually.

    Using a parser library

    # https://github.com/faisalman/ua-parser-js.
    
    var parser = new UAParser();
    var result = parser.getResult();
    
    var name = result.browser.name;
    var version = result.browser.version;
    

    Raw approach with Javascript

    # Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) \
    # Chrome/42.0.2311.135 Safari/537.36 Edge/12.10136
    
    window.navigator.userAgent.indexOf("Edge") > -1
    

提交回复
热议问题