Is it possible to change the userAgent?

9 posts, 0 answers
  1. Jan-Dirk
    Jan-Dirk avatar
    327 posts
    Member since:
    Jun 2012

    Posted 31 Oct 2012 Link to this post

    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.
  2. Jordan
    Admin
    Jordan avatar
    197 posts

    Posted 31 Oct 2012 Link to this post

    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.
  3. Jan-Dirk
    Jan-Dirk avatar
    327 posts
    Member since:
    Jun 2012

    Posted 31 Oct 2012 Link to this post

    Like you predicted, this didn't work. It was worth the try though!

    Thank you Jordan!
  4. Jordan
    Admin
    Jordan avatar
    197 posts

    Posted 31 Oct 2012 Link to this post

    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.
  5. Jan-Dirk
    Jan-Dirk avatar
    327 posts
    Member since:
    Jun 2012

    Posted 31 Oct 2012 Link to this post

    That would be wonderful. The page is http://www.hobbyking.com

    Your help is appreciated!
  6. Jan-Dirk
    Jan-Dirk avatar
    327 posts
    Member since:
    Jun 2012

    Posted 31 Oct 2012 Link to this post

    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
  7. Jordan
    Admin
    Jordan avatar
    197 posts

    Posted 31 Oct 2012 Link to this post

    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.

  8. Jan-Dirk
    Jan-Dirk avatar
    327 posts
    Member since:
    Jun 2012

    Posted 01 Nov 2012 Link to this post

    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.
  9. Eli
    Eli avatar
    1 posts
    Member since:
    Feb 2016

    Posted 02 Feb in reply to Jordan Link to this post

    thanks man
Back to Top