Solved

Load external website with different user agent string

Posted on 2014-03-11
5
1,250 Views
Last Modified: 2014-03-16
Hi

For educational propose.
I am trying to display an external website (with a mobile user agent string ) in my website.
I have googled around, and some ppl said its achievable using
a) iframe with javascript

http://stackoverflow.com/questions/14248446/setting-useragent-of-iframe-via-javascript

<script>  
$(function () {
        var frame = $('<iframe width="320" height="480"></iframe>');
        frame.hide();

        $('#container').append(frame);
        var contentWindow = frame[0].contentWindow || frame[0].contentDocument;

        var setUA = function() {
            if (Object.defineProperty) {
                Object.defineProperty(contentWindow.navigator, 'userAgent', {
                    configurable: true,
                    get: function () {
                        return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
                    }
                });
            } else if (Object.prototype.__defineGetter__) {
                contentWindow.navigator.__defineGetter__('userAgent', function () {
                    return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
                });
            } else {
                alert('browser not supported');
            }
        };

        $.ajax({
            cache: false,
            url: '/index.htm',
            success: function (html) {
                contentWindow.document.open();
                setUA();
                contentWindow.document.write(html);
                contentWindow.document.close();
                frame.fadeIn();
            }
        });
    });
</script>

Open in new window


b) cURL

But i can seem to make it work.
Note: totally newbie in these
0
Comment
Question by:Shirley80
5 Comments
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 39922468
Is there a purpose for this other than "education?"  What is the "value-add" of this?  What is it you are looking to do?  I am trying wrap my arms around this and cannot make sense of it.
0
 

Author Comment

by:Shirley80
ID: 39922648
It's for a file sharing website.
User won't need to login if the site is viewed in mobile browser.
thx
0
 
LVL 47

Accepted Solution

by:
dbrunton earned 300 total points
ID: 39922655
If you are using Firefox then try this addon

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

Enables you to switch to different User Agents which is what I think you want for testing your website.
0
 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 200 total points
ID: 39922680
Chrome Developer Tools will emulate quite a few phones including the different iPhones.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39932262
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This article discusses how to create an extensible mechanism for linked drop downs.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to count occurrences of each item in an array.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now