title: Phonegap Build CLI-5.2.0 Download and Close From Within Web App
body: <p>I've been wrestling with making calls to window open, using inappbrowser from within my app. Basically, I'm using phonegap as a wrapper to load up a mobile skinned CMS site with special app features. </p>
  
  <p>Here is the index.html. I'm using inappbrowser (with location set to no). </p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
  &lt;head&gt;
      &lt;title&gt;Emerald Test App&lt;/title&gt;
      &lt;meta name="apple-mobile-web-app-capable" content="yes" /&gt;
      &lt;meta name="viewport" content="width=device-width" /&gt; 
      &lt;script src="phonegap.js"&gt;&lt;/script&gt;
  
      &lt;script type='text/javascript'&gt;
        var ref = null;
        function onLoad() {
         document.addEventListener("deviceready", onDeviceReady, false);
        }
  
  
       function onDeviceReady() {
  
        var url = 'https://my-cms-site.com/content.aspx?page_id=31&amp;org_id=1&amp;app=1';
        var target = '_blank';
        var options = "location=no"
        ref = cordova.InAppBrowser.open(url, target, options);
  
       }
  
  &lt;/script&gt;
  &lt;/head&gt;
  
  
  &lt;body onload="onLoad()"&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>What I'm looking to do, is open links in a system browser - from my external site loaded through inappbrowser</p>
  
  <p>I've tried downloading files using similar documentation and suggestions from posts like</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
          window.open(url, '_system');
  &lt;/script&gt;
  </code></pre>
  
  <p>And _blank, and adding 'location=no' etc, but no dice. These are external pages, loaded from my remote site. </p>
  
  <p>When these links are clicked, they open in the same browser (inappbrowser or webview) and take over the browser. What I'm looking to do is open these up in another system browser (chrome, safari, whatever). This would take care of my download issue (as the files will hopefully open up in the system browser and the user can figure out what to do with them).</p>
  
  <p>I've tried to add an event listener, and executescript to return a value of the href. Then use that value to window.open(href,'_system'); from the index.html (instead of the remote page). Because on the index, I will still have the reference to inappbrowser.</p>
  
  <pre><code>   ref.addEventListener( "loadstop", function() {
     ref.executeScript(
          { code: "var gbal = null; $('a').on('click', function() { gbal = $(this).attr('href'); }); (function runIt() { return gbal })();" },
          function( values ) {
              if (values != null) {
              //alert( values[ 0 ] );
               window.open(values[0],'_system');
              }
  
          }
            );
         });
     }
  </code></pre>
  
  <p>The values[0] is always null. Which seems to indicate I'm not doing something correctly in the code: portion of executescript - or $(this) is not really <em>this</em></p>
  
  <p>So, big question - how can I open links in my external site in a system browser. window.open('whatever.htm', '_XXXXX') makes no difference when called on my remote site. Am I on the right track by using an event listener?</p>