14

I'm new to Javascript. I have found this code to change user agent using Javascript.

var __originalNavigator = navigator;
navigator = new Object();
navigator.__defineGetter__('userAgent', function () {
    return 'Custom';
});

var iframe='<iframe id="frame" name="widget" src ="http://www.useragentstring.com/" width="100%" height="400" marginheight="0" marginwidth="0" frameborder="no" scrolling="no"></iframe>';        

document.write("User-agent header sent: " + navigator.userAgent + iframe);   

This code works & returns fake user agent, Though how will I set same fake user agent for iframe ?

Here is fiddle of what I'm up to : http://jsfiddle.net/ufKBE/1/

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
MANnDAaR
  • 2,546
  • 7
  • 45
  • 64
  • How about document.getElementById("frame").navigator.... – mplungjan Aug 21 '12 at 15:10
  • Thanks but can you elaborate more? My Javascript skills are too bad.. – MANnDAaR Aug 21 '12 at 15:17
  • 2
    @mplungjan - Frames have no navigator property. I don't think this is actually possible. – PhonicUK Aug 21 '12 at 15:20
  • 2
    You are changing navigator.userAgent, which is just a javascript property. You are not actually sending a header with that user agent string anywhere. You can set headers for ajax calls using setRequestHeader (http://stackoverflow.com/questions/1268673/set-request-header-in-javascript), but only append to a UA string. I doubt that there's a way to modify headers for an iframe. – undefined Aug 21 '12 at 15:25
  • If window has a navigator that is read/write, iframe could have one too, no? – mplungjan Aug 21 '12 at 15:34

2 Answers2

5

I already answer the same question at <Load iframe content with different user agent>

For your convenient, I copied and paste the answer here:

First of all, you must create a function to change the user agent string:

function setUserAgent(window, userAgent) {
    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
            });
        }
    }
}

Then you need to target the iframe element:

setUserAgent(document.querySelector('iframe').contentWindow, 'MANnDAaR Fake Agent');

You may also set an ID to the iframe and target the ID instead of all iframe elements on the page.

Community
  • 1
  • 1
Aero Wang
  • 8,382
  • 14
  • 63
  • 99
2

That is not the right way to switch your user agent to the faked one. window.navigator = {userAgent:Custom_User_Agent} is just a javascript execution. It will simply be ignored as you refresh the page, either it is on window or within the iframe, and then the default user agent which will be sent to the server instead. If you really want to switch your user agent, it has to be the browser setting you deal with. Some browsers allow this on their settings, and some others include user agent switcher or support some kind of plugin that do this

http://www.howtogeek.com/113439/how-to-change-your-browsers-user-agent-without-installing-any-extensions/

The alternatives are, you can also try to access the website from the server or build your own web accessing application. These ways, you can freely alter your header or use your own customized user agent

Another way is by using AJAX. but of course it is limited by cross-origin-policy

Dingredient
  • 2,191
  • 22
  • 47
stackunderflow
  • 1,492
  • 1
  • 23
  • 53
  • 4
    This doesn't answer the question. The original question is about how to fake the user agent in code, particularly how to set the user agent for an iframe. You're providing information on how a user can change their user agent in the browser manually. – Chris Halcrow Nov 13 '15 at 06:16