I tried the following code (code is from here) which can change the value of navigator.userAgent
in browsers, and it works fine! after running this code, when I check the value of navigator.userAgent
, it is changed to the new value. but then when I send a post request with ajax
, the user-agent
header which is automatically sent with my request, has not changed and is the actual user agent of the browser. why the user agent has not changed for ajax request?
function setUserAgent(window, userAgent) {
// Works on Firefox, Chrome, Opera and IE9+
if (navigator.__defineGetter__) {
navigator.__defineGetter__('userAgent', function () {
return userAgent;
});
} else if (Object.defineProperty) {
Object.defineProperty(navigator, 'userAgent', {
get: function () {
return userAgent;
}
});
}
// Works on Safari
if (window.navigator.userAgent !== userAgent) {
var userAgentProp = {
get: function () {
return userAgent;
}
};
try {
Object.defineProperty(window.navigator, 'userAgent', userAgentProp);
} catch (e) {
window.navigator = Object.create(navigator, {
userAgent: userAgentProp
});
}
}
}