Search code examples
cordovainappbrowser

PhoneGap InAppBrowser data exchange


Is it possible to launch website from PhoneGap application in InAppBrowser and exchange data with it? What I in particular would like to do is to add an event listener for form's submit and get the form data to the application running InAppBrowser.

Also, if the data exchange isn't possible, what is the best way to show the series of HTML pages loaded from online to local cache and apply some JavaScript handling on them, just a bare iframe?


Solution

  • You can read the URL of the page opened in InAppBrowser. In your case, if you would have a form with method GET, you will have all the details placed in the URL after submitting it.

    For example:

    <form type="submit" method="GET">
       <input type="text" name="foo" />
       <input type="submit" value="Submit" />
    </form>
    

    Then if you enter, some value, say "bar" and hit "Submit", you will get:

    <your_url_path_to_file>?foo=bar
    

    And finally you will be able to read it in JavaScript:

    var authWindow = window.open('<your_url_path_to_file>', '_blank', 'location=no,toolbar=no');
    
    authWindow.addEventListener('loadstart', function(event) {
       var url = event.url;
       var foo = /\?foo=(.+)$/.exec(url);
    });