Control User Agent to call iframe

Asked

Viewed 236 times

1

Good morning!! I was doing some things here and decided to put an iframe on the page, but it was loaded with the User Agent from some other browser. Just to understand the behavior of the site.

This is possible with HTML5 and Javascript?

Otherwise, can I do this with Curl in PHP? And how do I do?


I was able to do this with Curl, but I wanted to do it with iframe. The solution, in PHP, was:

<?php
$url = "http://example.com";
$ch = curl_init();
$curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
curl_exec($ch);    

Playing all this in a DIV, I get something, but I want to do with iframe, because then I have problem with images, which I solved with a base tag, but I prefer iframe.

  • Are you referring to joining an iframe here on [en.so]? or on a page of your own? What do you mean "but that it was loaded with the User Agent from some other browser"?

  • Tested my suggestion with iframe Heliton?

  • I tested yes, it worked as expected. Thank you very much.

  • I forgot to thank you because I went to test and forgot kkkk

1 answer

2


To change the user agent of a iframe, or a page is possible with javascript as follows:

Set the function to set the user agent:

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 select the object window from where you want to change the user agent:

var mWindowFrame = document.querySelector('iframe').contentWindow;

Call the function by passing the object window and the new value of user agent to be set:

setUserAgent(mWindowFrame, 'Meu User Agent falso!');

Ready from that point the value of user agent will be what you set!

Complete example in jsFiddle

Browser other questions tagged

You are not signed in. Login or sign up in order to post.