This is a migrated thread and some comments may be shown as answers.

Is it possible to change the userAgent?

8 Answers 1667 Views
HTML5, CSS, JavaScript
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jan-Dirk
Top achievements
Rank 1
Jan-Dirk asked on 31 Oct 2012, 06:58 AM
I have the need to change the user agent information that Cordova uses. In my app I have an iframe that loads a website. That site shows an JS confirm() asking if you want to move to their mobile site which I don't want for my site.

If there was the possibility to change the user agent, then I wouldn't get the confirm box. I tried this:
navigator.userAgent = "test";
to no avail. Code doesn't break, but after check the value of userAgent it is still the original value.

If anyone knows a different way to get rid of the confirm box from the source of that page before it is shown, then that would be fine too.

8 Answers, 1 is accepted

Sort by
0
Jordan
Telerik team
answered on 31 Oct 2012, 08:29 AM
Hi Jan-Dirk,

To change the user agent you can use this (replace sample string with yours):

window.navigator.__defineGetter__('userAgent', function () {
    return 'Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206';
});

but i'm afraid it will not work for the nested iframe.

Kind regards,
Jordan
the Telerik team

Share feedback and vote for features on our Feedback Portal.
Want some Kendo UI online training - head over to Pluralsight.
0
Jan-Dirk
Top achievements
Rank 1
answered on 31 Oct 2012, 09:05 AM
Like you predicted, this didn't work. It was worth the try though!

Thank you Jordan!
0
Jordan
Telerik team
answered on 31 Oct 2012, 09:34 AM
Hi,

Disusing  our case here one of my colleges suggested to use jQuery.get to get the page source the remove the code that checks browser agent. And insert the processed page.

If you tell me what the site you put in iframe i can give it a try too.

Greetings,
Jordan
the Telerik team

Share feedback and vote for features on our Feedback Portal.
Want some Kendo UI online training - head over to Pluralsight.
0
Jan-Dirk
Top achievements
Rank 1
answered on 31 Oct 2012, 09:37 AM
That would be wonderful. The page is http://www.hobbyking.com

Your help is appreciated!
0
Jan-Dirk
Top achievements
Rank 1
answered on 31 Oct 2012, 09:46 AM
Some additional info: I have tried to modify the html of that page before, 2 ways:
- Downloading the page with an xmlhttprequest and inserted it in the iframe.innerHTML, but since things like images and loading stylesheets in the html of that page were written without the domain, I had to add that by code myself (example: <img src="catalog/img0001.jpg" /> had to converted to <img src="http://www.hobbyking.com/catalog/img0001.jpg" />

- assigning the url to the iframe's src attribute and using the frame.onload method to remove the confirm box resulted that the confirm box was still shown and then redisplayed.

I used this code to remove the confirm:
text = text.replace('function switchtomobile()', 'function dontswitch2mobile()'); // rename the function so code doesn't break on next command
text = text.replace('switchtomobile()', ''); // remove the call to the function
0
Jordan
Telerik team
answered on 31 Oct 2012, 05:02 PM
Hi Jan-Dirk,

You can solve the first problem by adding this to top of the page source.

<base target="_self" href="http://hobbyking.com/">

It will automatically make all links to open in same container to open from this base and in same iframe

As for the second approach frame.onload is too late their script is invoked before that event fire.
All the best,
Jordan
the Telerik team

Share feedback and vote for features on our Feedback Portal.
Want some Kendo UI online training - head over to Pluralsight.

0
Jan-Dirk
Top achievements
Rank 1
answered on 01 Nov 2012, 12:22 PM
Hello Jordan,

you suggestion to use base target have solved a big part of the issue. I was also able to remove the alert() this way. My only issue is that consecutive pages still give the alert() and I don't think I can avoid that.

Also, it seems though that form-submits don't work. On each product page of the site I include in my app, there is an 'add to cart' button which does a form post that is never fired. Normal links work ok

Anyway, thank you for your help. I think I have to find a different way of displaying products in my app.
0
Eli
Top achievements
Rank 1
answered on 02 Feb 2016, 11:16 AM
thanks man
Tags
HTML5, CSS, JavaScript
Asked by
Jan-Dirk
Top achievements
Rank 1
Answers by
Jordan
Telerik team
Jan-Dirk
Top achievements
Rank 1
Eli
Top achievements
Rank 1
Share this question
or