4

Following code changes property printed (when accessed through javaScript) by browser console and not the actual Use-Agent string sent with the header.

Object.defineProperty(navigator, 'userAgent', {
    get: function () { return 'Mozilla/5.0 (Windows NT 6.2; WOW64; 
    rv:28.0) Gecko/20100101 Firefox/28.0)'; }
});
4
  • 1
    A Navigator object can be retrieved using the read-only window.navigator property. developer.mozilla.org/en-US/docs/Web/API/Navigator Commented Sep 19, 2019 at 7:44
  • However stackoverflow.com/questions/2166540/… Commented Sep 19, 2019 at 7:46
  • Firefox and Safari just let you use XHR.setRequestHeader('User-Agent', 'whatever'). Chrome on the other hand throws an error... Commented Sep 19, 2019 at 12:19
  • This is now allowed, see my answer Commented Dec 27, 2020 at 19:54

2 Answers 2

5

This is now possible to do, see: https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name You can use ‘ XMLHttpRequest.setRequestHeader(User-Agent, value)’

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

2 Comments

Although it should be possible, it does seems like Chromium is behaving like old IE - they make there own rules. I tested both Chrome and Edge - User-Agent is being ignored and controlled by said browsers.
Chromium responds with this in the console: Refused to set unsafe header "User-Agent".
3

This isn't possible from the page itself. The user agent string (as sent in the HTTP request headers) can only be changed via browser extension or browser configuration.

1 Comment

I trust you are right but maybe you could provide a link to so something that confirms your statement

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.