3

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() {
  
}
3
  • window.navigator.userAgent Commented Jul 29, 2020 at 7:02
  • Do you want to know what kind of browser users are accessing? Commented Jul 29, 2020 at 7:03
  • @MinwooKim yes, Chrome Commented Jul 29, 2020 at 7:05

2 Answers 2

5

Just use window.navigator.userAgent

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

console.log(userAgent());

Sign up to request clarification or add additional context in comments.

5 Comments

I get this error: ` return window.navigator.userAgent;` ^ ReferenceError: window is not defined I should clarify that I also don't want to use html for this
How do you run this code? Are you using Node maybe?
I am using node
Well, then you should definitely clarify that in the question because that changes like everything. You cannot get a user agent in the Node app itself because UA is something tightly connected with a client app. You could do that if you had for example some frontend app that makes a request to your Node server - then you can get the UA header from the request.
You have reference this article stackoverflow.com/questions/6163350/…
1

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.