16 Replies Latest reply on Dec 1, 2010 11:59 AM by SClukey

    Default browser window from HTML

    StudioBQ Level 1

      I am just building a simple HTML app and want to open a new window to an existing web page from the air app that opens in a default browser window rather than spawining a new air window.

       

      I have been banging my apparently limited-capacity head against a wall for 8 hours now with no luck.

       

      Can anyone tell me how to get this done? I am guessing it's as simple as adding a piece of code to my app xml file and a corresponding piece of code to my HTML page where I have the link?

       

      Any help will be sincerely appreciated.

      Bill

        • 1. Re: Default browser window from HTML
          Joe ... Ward Level 4

          It is very simple if that's what you want all links to open in the user's default browser. Simply set the HTMLLoader property navigateInSystemBrowser to true.

           

          window.htmlLoader.navigateInSystemBrowser = true;

           

          Now, if you only want that to happen for some links, then there are probably a couple of ways to do it. Essentially, you would need to capture the click on the link (or a related event), get the target URL and use the flash.net.navigateToURL() function.

           

          window.runtime.flash.net.navigateToURL( new window.runtime.flash.net.URLRequest( urlString ) );

          • 2. Re: Default browser window from HTML
            StudioBQ Level 1

            Hi, Joe - thank you for the rapid reply.

             

            I appreciate your tip very much - and forgive my ignorance as this is my first whack at an AIR app...but can you possibly clarify WHERE I put this? After digging and digging today, I am guessing this goes in my AIRaliases.js file - yes? - and if so, where do I add/edit this?

             

            Again - my apologies. This, sadly, is how I learn.

            Bill

            • 3. Re: Default browser window from HTML
              Joe ... Ward Level 4

              For option A, you would need to set the navigateInSystemBrowser for every HTML window in your app, so doing it in a script tag in the head of each page in your app should work (or in an imported script).  Similarly, for option B, you would either need to futz with event handlers or change the links in the DOM, so again, a script that ran when the page loads is probably the right place.

               

              I wouldn't use AIRAliases.js, though. That file might change in future releases of the AIR SDK (as new features are added), so you should leave it as is to make it easier to upgrade. Your own external script file, imported the same way should be fine.

              • 4. Re: Default browser window from HTML
                StudioBQ Level 1

                So...and again, please forgive me:

                 

                I place this in the head of every HTML page I want a link to open in the default browser:

                 

                   <script>
                        window.htmlLoader.

                navigateInSystemBrowser = true;


                    </script>

                 

                Is that correct?

                 

                And do I have to append my link in any way to invoke the script, or does it just apply automatically to the links on a page where this is included in the head?

                • 5. Re: Default browser window from HTML
                  Joe ... Ward Level 4

                  Yes that will work. It will apply to all links in all pages loaded into that htmlLoader object. (So if you loaded a new page in the same AIR window, its links would also open in the default browser, even if that page didn't have the script.) And no, you don't have to change the links in any way.

                  • 6. Re: Default browser window from HTML
                    StudioBQ Level 1

                    Wonderful - thank you very much.

                     

                    I appreciate your patience and clear guidance.

                    Bill

                    • 7. Re: Default browser window from HTML
                      MattNico

                      I have a similar problem:

                       

                      I have built a web app for the iPhone which works splendidly in AIR, except that it's links to external sites will not open in the default browser.

                       

                      Currently I have one HTML file in the AIR app: redirect.html which simply redirects to my web app.

                       

                      If I set <script>window.htmlLoader.navigateInSystemBrowser = true;</script> the redirect to my web app opens in Firefox instead of AIR. If I set that on the home page (and all subpages) of my web app it's as if nothing was ever set.

                       

                      I am new to AIR so I am making these assumptions:

                      1. window.htmlLoader.navigateInSystemBrowser = true; must be set on the first page loaded by the AIR app.
                      2. server-hosted pages cannot open anything in the default browser.

                       

                      Is there are a way to set it up so my AIR version of the web app can simply mirror locally the code from my server, thus allowing me to access the default browser? I have tried an iframe which actually just caused the web app to load in Firefox like the redirect.

                       

                      Any advice would be helpful.

                       

                      Thanks.

                      • 8. Re: Default browser window from HTML
                        Joe ... Ward Level 4

                         


                        AIR has two types of sandboxes. Code installed as part of your application runs in the application sandbox and has full access to all of the AIR APIs (such as navigateInSystemBrowser). Code loaded from elsewhere, including elsewhere in the file system and Internet, is loaded into a remote sandbox based on its domain of origin. This is why the pages you load from your server can't set the navigateInSystemBrowser property.

                         

                        What you might try is loading your web page into an iframe in the redirect.html document, but not setting navigateInSystemBrowser to true until the page has started loading (or maybe you can use an earlier event like DOMInitialize).

                        • 9. Re: Default browser window from HTML
                          MattNico Level 1

                          Joe,

                           

                          Thanks for you quick response.

                           

                          I tried setting a five second delay on initializing the navigateInSystemBrowser. Page loaded fine, but then all the links opened within the iFrame and I also lost my resizing inhibition...

                           

                          Frankly I'm not sure what a DOMInitialize is. I'm kind-of a n00b.

                           

                          -Matt

                          • 10. Re: Default browser window from HTML
                            MattNico Level 1

                            OK, I made some progress but I am one step shy of having the full solution. Here was my solution:

                             

                            <script type="text/javascript">
                            <!--
                            function delayer(){
                                window.location = "http://mywebapp.com/"
                                setTimeout("window.htmlLoader.navigateInSystemBrowser = true;", 1);
                            }
                            //-->
                            </script>

                             

                            </head>
                            <body onLoad="delayer()">

                             

                            The only problem is that even relative links now load in the system browser. I need relative links to load in the AIR app and absolute (http://, etc.) links to load in the system browser. Any thoughts?

                             

                            Thanks in advance.

                             

                            -Matt

                            • 11. Re: Default browser window from HTML
                              Joe ... Ward Level 4

                              What happens if you set a target for the links?

                               

                              If you want different behavior on the web than in your AIR app, then you could add some code to the AIR app that walks through the DOM to find links and sets a target so the link opens in a new window (assuming a target fixes things). Or you could listen for click events and override them. The Flash navigateToURL function can be used to open a URL in the user's default browser.

                               

                              If you don't mind compiling a little ActionScript, you can extend the HTMLHost class to control how links work. See http://help.adobe.com/en_US/AIR/1.5/devappshtml/WS5b3ccc516d4fbf351e63e3d118666ade46-7e74. html

                              • 12. Re: Default browser window from HTML
                                MattNico Level 1

                                Setting targets makes no difference whatsoever.

                                 

                                That link to extending the HTMLHost class looks promising. Thanks for the help. I'll let you know if I need anything further.

                                 

                                Thanks again for your guidance.

                                 

                                -Matt

                                • 13. Re: Default browser window from HTML
                                  MattNico Level 1

                                  THANK YOU, THANK YOU, THANK YOU! (Yes, I am shouting—for joy!)

                                   

                                  I got the Actionscript working. It took a lot of experimentation and some odd workarounds but it works. I've posted an example so others with the same issues can benefit from your help and my experimentation as well.

                                   

                                  Cheers,

                                  Matt

                                  • 14. Re: Default browser window from HTML
                                    Leonin9

                                    Hi MattNico,

                                     

                                    Do u mind to tell where can I find the example u posted?

                                     

                                    Thanks.

                                    • 15. Re: Default browser window from HTML
                                      chris.campbell Adobe Employee

                                      Moved discussion to the Adobe AIR Development forum

                                      • 16. Re: Default browser window from HTML
                                        SClukey

                                        Well I don't quite know how he is doing it, but this way works for me.

                                         

                                        In your ActionScript put this

                                         

                                         

                                        myHTMLLoader.addEventListener(Event.HTML_DOM_INITIALIZE, onDomInit);
                                        
                                        function onDomInit(event:Event):void {
                                             myHTMLLoader.window.airBrowseInDefault = browseInDefault;
                                        }
                                        
                                        function browseInDefault(url:String):void {
                                             navigateToURL(new URLRequest(url));
                                        }
                                        

                                         

                                         

                                        Then, whenever you want to open a url in the default browser, just use this in your html

                                         

                                         

                                        <a href="javascript: airBrowseInDefault('http://someURL.com/');">Link text</a>