Skip to content Skip to sidebar Skip to footer

Fake User Agent For Iframe

I'm new to Javascript. I have found this code to change user agent using Javascript. var __originalNavigator = navigator; navigator = new Object(); navigator.__defineGetter__('user

Solution 1:

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:

functionsetUserAgent(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.

Solution 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

Post a Comment for "Fake User Agent For Iframe"