简体   繁体   中英

Trouble with Navigator.appCodeName and Useragent

While executing below code it was displaying Mozilla eventhough if it was executed in safari or chrome at Useragent and AppCode Name. Am i missing anything?

   <!DOCTYPE html>
    <html>
    <body>

    <p>Click the button to display the code name of your browser.</p>

    <button onclick="myFunction()">Try it</button>

    <p id="demo"></p>
    <p id="demo1"></p>

    <script>
    function myFunction() {
        var x = "Browser CodeName: " + navigator.appCodeName;
        var y = navigator.userAgent;

        document.getElementById("demo").innerHTML = x;
        document.getElementById("demo1").innerHTML = y;

    }
    </script>

    </body>
    </html>

navigator.appCodeName always returns 'Mozilla' on any browser because the property is deprecated and kept only for compatibility purposes.
https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID.appCodeName

A library like https://github.com/ded/bowser will solve the problem for most browser detection.
This library is more exhaustive: https://github.com/bestiejs/platform.js/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM