17 Replies Latest reply on Feb 10, 2009 7:36 AM by

    Setting User agent ?

      I am making a small AIR app that shows an HTML page. How do I set the user agent ?
      I do not want to use the default one. I went through this: http://help.adobe.com/en_US/AIR/1.1/devappsflex/WS5b3ccc516d4fbf351e63e3d118666ade46-7e7b. html

      But a sample code or explanation would be good.

        • 1. Re: Setting User agent ?
          Cesare Rocchi Level 2
          As the documentation says try setting URLRequestDefaults.userAgent like this:

          URLRequestDefaults.userAgent = "MyClientName";

          This is a global setting for ALL the requests.

          If you want to change it in a single html:

          var loader: HTMLLoader = new HTMLLoader();
          loader.userAgent = "MyClientName";
          loader.load ...

          1 person found this helpful
          • 2. Setting User agent ?
            I have made a file called: app.js
            Contents:
            function useragent()
            {
            URLRequestDefaults.userAgent = "AGENT";
            }

            And the main app.html file has:

            <html>
            <head>
            <script src="AIRAliases.js" language="JavaScript" type="text/javascript"></script>
            <script src="app.js" language="javascript" type="text/javascript"></script>
            </head>
            <body id="body" onload="useragent()">
            //CONTENT
            </body>
            </html>

            However, the useragent is still default.

            Any ideas where am I going wrong?
            • 3. Re: Setting User agent ?
              Cesare Rocchi Level 2
              Sorry I thought you were programming via Flex.

              In any case the statement is correct.

              Can't you debug the program? Does the function "useragent()" get called?


              1 person found this helpful
              • 4. Setting User agent ?
                Yes the function is being called.

                The console gives the foll. error:
                Reference Error: Can't find variable: URLRequestDefaults

                I have included AIRAliases.js

                Do I need any other file?

                • 5. Re: Setting User agent ?
                  Cesare Rocchi Level 2
                  Sorry I am not an expert of Air applications built with Html/JS.
                  Did you check whether .js is included?

                  -c.
                  1 person found this helpful
                  • 6. Re: Setting User agent ?
                    Yes the AIRAliases.js is included. However, from the error, I think that one more file has to be linked.... As the resouce cannot be found.

                    Can you let me know from where do I get all other API files like AIRAliases. I guess that would solve the issue.

                    Thanks.
                    • 7. Re: Setting User agent ?
                      Cesare Rocchi Level 2
                      I can just guess because, as said, I am not an expert of HTML/JS air apps.

                      Try putting your function in line, to see if ti works. Like this:

                      <script>
                      function useragent()
                      {
                      URLRequestDefaults.userAgent = "AGENT";
                      }
                      </script>

                      If this works the issue is related to app.js not being loaded correctly.

                      HTH
                      • 8. Setting User agent ?
                        Tried it. Still the same message. The issue is not with loading the app.js. It is because the js file by Adobe which has the variable URLRequestDefaults is not linked to the html. As I don't have the file.

                        Now the question is, which file has the function.

                        • 9. Re: Setting User agent ?
                          jbenson@oper8 Level 2
                          Try this:

                          air.URLRequestDefaults.userAgent = "MyClientName";
                          var winHandler = air.HTMLLoader.createRootWindow(true);
                          winHandler.load(new air.URLRequest(" http://showip.net/check_user_agent.php"));
                          • 10. Re: Setting User agent ?
                            Thanks a lot! It worked !!

                            However, there's only one problem. A new window opens with the new user agent and the main window is blank. I need only 1 window. How do I remove the main window and keep the one with new user agent ?

                            Thanks.

                            • 11. Re: Setting User agent ?
                              jbenson@oper8 Level 2
                              once you call air.URLRequestDefaults.userAgent and set it to whatever you want your user agent to be, all URLRequests should use that user agent.

                              I just included the createRootWindow call so that you could see the userAgent in action easily.

                              The only problem with your original code was that you were not calling the air alias (air.URLRequestDefaults vs URLRequestDefaults)

                              If you wanted to call it w/o loading the AIRAliases.js file then you would need to set the parameter using:
                              window.runtime.flash.net.URLRequestDefaults = "YOURAGENT";

                              • 12. Re: Setting User agent ?
                                The new window has the defined user agent and the main window still shows AIR 1.5 user agent.

                                • 13. Re: Setting User agent ?
                                  jbenson@oper8 Level 2
                                  Maybe I just misunderstand.

                                  Can you post some more code?

                                  What method are you using to load html into the main window?
                                  • 14. Re: Setting User agent ?
                                    IFRAME
                                    • 15. Re: Setting User agent ?
                                      jbenson@oper8 Level 2
                                      Apologies for wasting your time.

                                      Because the iframe is not in the application security sandbox I don't see how you could use the air apis to affect the user agent without creating a sandbox bridge.

                                      I have never discovered a way to get HTMLLoader (and by definition URLRequest) to "target" an iframe (or dump contents into the iframe) but surely there must some way. Without being able to let the application supply the contents of the iframe I don't believe you'll be able to set the user agent.

                                      Sorry I don't know of any method. Good Luck!

                                      • 16. Re: Setting User agent ?
                                        I don't have to use an iFrame. I just want to display a web page in the app. You can let me know on how to display an HTML page in the body instead of using the iFrame.

                                        In that case, the user agent would work and I could work further.

                                        Thanks.

                                        • 17. Re: Setting User agent ?
                                          I called the js function on IFRAME load and it worked!!