Search code examples
javascriptcordovaphonegap-pluginsphonegap-buildphonegap-desktop-app

phonegap and cordova not opening link in app browser


I am new to PhoneGap and Cordova, I am trying to create a wrapper application script for my website.

However the problem I am getting is that, when I build the app using PhoneGap cloud and click on a link on an external link in the page it opens the link in Chrome and not the native browser, do I need to install anything? Like a plugin or something?

I have checked that the plugins are in the project that PhoneGap desktop app created for me, however do I need to copy them in to the www folder?

/plugins/cordova-plugin-inappbrowser

My code is simple and I am trying a few different methods to open it in the native browser:

<div id="deviceready" class="blink">
    <h1>Test App</h1>
    <a href='#' onclick='navigator.app.loadUrl("http://www.tutorialspoint.com/", {openExternal : false});'/>Test</a><br/>
    <a href='#' onclick="navigator.app.loadUrl('http://www.tutorialspoint.com/', { openExternal:true });">Link</a><br/>
    <a href="#" onclick="window.open('http://www.tutorialspoint.com/', '_system');">system</a><br/>
    <a href='http://www.tutorialspoint.com/'/>Google</a><br/>
    <a href='newpage.html'/>newpage</a><br/>
</div>

Solution

  • First you need to initialize the plugin like so:

    document.addEventListener("deviceready", onDeviceReady, false);
    function onDeviceReady() {
        window.open = cordova.InAppBrowser.open;
    }
    

    This is taken from the https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/

    _blank: Opens in the InAppBrowser.

    _system: Opens in the system's web browser.

    Therefor you need to change :

    <a href="#" onclick="window.open('http://www.tutorialspoint.com/', '_system');">system</a><br/>
    

    To this :

    <a href="#" onclick="window.open('http://www.tutorialspoint.com/', '_blank', 'location=yes');">system</a><br/>