33

Is there any way to do this? I'm trying to send a GET request to a website, but I want to customize my UserAgent. Is there any way to do this in pure HTML and JavaScript? I'd like it to all execute locally.

1

5 Answers 5

20

This is working for me.

Object.defineProperty(navigator, 'userAgent', {
    get: function () { return 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0)'; }
});

It is an updated version of code4coffee's answer as Object.prototype.__defineGetter__() is deprecated: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/__defineGetter__

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

2 Comments

This is not working for me right now in Windows Google Chrome Version 78.0.3904.108 (Official Build) (64-bit)
@Ryan because this header is now read-only
10

You can programmatically do this in Javascript (this example mocks up Firefox):

navigator.__defineGetter__('userAgent', function () {
    return "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0)"
});
navigator.__defineGetter__('appName', function () {
    return "Netscape"
});

You can then view the changes in the console via (and of course check these via Javascript):

navigator.userAgent
navigator.appName

Here's an example of a test that should work (using Jasmine):

describe("isUserAgentInternetExplorer", function () {
    it("should return false for Firefox", function () {
        navigator.__defineGetter__('userAgent', function () {
            return "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0)"
        });
        navigator.__defineGetter__('appName', function () {
            return "Netscape"
        });
        //your code here...
        expect(...your code here...).toEqual(false);
    });
});

Comments

6

2022

You are not allowed to modify the following headers, according to Fetch WHATWG spec.
And in 2022 all the evergreen browsers have implemented this requirement.

`Accept-Charset`
`Accept-Encoding`
`Access-Control-Request-Headers`
`Access-Control-Request-Method`
`Connection`
`Content-Length`
`Cookie`
`Cookie2`
`Date`
`DNT`
`Expect`
`Host`
`Keep-Alive`
`Origin`
`Referer`
`TE`
`Trailer`
`Transfer-Encoding`
`Upgrade`
`Via`

As well as headers, starting with proxy- or sec-.

These are forbidden so the user agent remains in full control over them.

2024

The User-Agent header is not forbidden anymore:

The User-Agent header is no longer forbidden, as per spec — see forbidden header name list (this was implemented in Firefox 43) — it can now be set in a Fetch Headers object, or with the setRequestHeader() method of XMLHttpRequest. However, Chrome will silently drop the header from Fetch requests (see Chromium bug 571722)

10 Comments

User-Agent is missing from that list.
@ctn Not seeing User-Agent under the linked documentation as being forbidden!
@phuzi yes, not forbidden, so allowed, yes?
@ctn But the answer lists forbidden headers, not allowed headers! So User-Agent should not be in the list. Your comment sounds like you think that it should be added to the list in the answer.
@phuzi I am confused. This answer references that list of headers you are not allowed to modify. The question is about the User-Agent header. What's the connection? (if anything, the fact that User-Agent is not in that list I gather means that it is allowed to modify it)
|
-1

If you are using a XMLHttpRequest you can set a custom Request Header like:

var xhr = new XMLHttpRequest(...);
xhr.setRequestHeader("User-Agent","test");

2 Comments

I'm sorry but Chromium complains: Refused to set unsafe header "User-Agent
Idem with FF31.
-5

You will never change a user Agent in HTML, html is the message not messenger.

Indeed you can do it with a javascript code, but it's dangerous if you ever deploy it in production.

It's far safer to use an agent switcher:

https://addons.mozilla.org/fr/firefox/addon/user-agent-switcher/

https://chrome.google.com/webstore/detail/user-agent-switcher-for-c/djflhoibgkdhkhhcedjiklpkjnoahfmg

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.