Search code examples
javascriptuser-agent

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() {
  
}

Solution

  • 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>