简体   繁体   中英

How to get HTTP user-agent header with js?

For my project, I want to get and print the user-agent of my browser. I'm using javascript (with node) to accomplish my goal. How can I get and print the user-agent header without using the HTTP module and html if possible and inside of this blank function:

function userAgent() {
  
}

Just use window.navigator.userAgent

 function userAgent() { return window.navigator.userAgent; } console.log(userAgent());

You can try this.

<script>
   window.onload = function () {
        var agent = navigator.userAgent.toLowerCase();
        if (agent.indexOf('chrome') != -1) { 
           alert('Chrome');
        }
        if (agent.indexOf('msie') != -1) {
           alert('Explorer');
        }
        if (agent.indexOf('safari') != -1) {
           alert('Safari');
        }
        if (agent.indexOf('firefox') != -1) {
           alert('Firefox');
        }
   }
</script>

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