Opening External Links in System Browser with PhoneGap Applications

Dec 02, 2025 · Programming · 9 views · 7.8

Keywords: PhoneGap | Cordova | InAppBrowser

Abstract: This article addresses the common issue in PhoneGap/Cordova applications where HTML links open within the app instead of the system browser. It provides a comprehensive solution using the InAppBrowser plugin, detailing the use of window.open with _target parameter, code examples, and plugin installation. The article also explores optimized event interception techniques for more flexible external link handling.

Problem Background and Root Cause Analysis

In PhoneGap/Cordova hybrid application development, developers frequently encounter a common issue: HTML hyperlinks within the application open by default in the app's own WebView container rather than in the device's system browser (such as Safari on iOS or Chrome on Android). This creates user experience problems, particularly when users need to return to the app and may become trapped due to lack of navigation mechanisms.

The root cause lies in the PhoneGap application runtime environment. When using standard HTML anchor tags (<a>), even with attributes like target="_blank" or data-rel="external", links still load within the application's internal WebView. This occurs because PhoneGap applications are essentially native apps encapsulating a WebView, where all network requests are processed within this container by default.

Core Solution: The InAppBrowser Plugin

To resolve this issue, the Cordova InAppBrowser plugin must be utilized. This plugin provides an extended version of the window.open() method, allowing developers to specify where links should open. The crucial parameter is _system, which directs links to open in the device's default system browser.

First, the InAppBrowser plugin must be installed. This can be done using the Cordova command-line tool:

cordova plugin add cordova-plugin-inappbrowser

After installation, external links can be opened using the following JavaScript approach:

<a href="#" onclick="window.open('http://www.example.com', '_system'); return false;">Visit Website</a>

This code works by: when the user clicks the link, the onclick event handler calls the window.open() method, with the first parameter being the target URL and the second parameter '_system' specifying opening in the system browser. return false prevents the default link behavior, avoiding opening within the app.

Optimization: Event Interception Mechanism

While the above method solves the problem, manually adding onclick handlers to every link is inefficient in practical applications. A better approach is to use event delegation to intercept click events for all external links.

Here is an optimized JavaScript implementation:

document.addEventListener('DOMContentLoaded', function() {
    document.addEventListener('click', function(e) {
        var target = e.target;
        while (target && target.nodeName !== 'A') {
            target = target.parentNode;
        }
        if (target && target.nodeName === 'A' && 
            (target.getAttribute('target') === '_blank' || 
             target.getAttribute('data-rel') === 'external')) {
            e.preventDefault();
            window.open(target.href, '_system');
        }
    });
});

This code uses event bubbling to detect all click events. When the clicked element is an anchor tag (<a>) with target="_blank" or data-rel="external" attributes, it prevents the default behavior and opens the link in the system browser using window.open(). This method is more flexible as it doesn't require modifying the HTML code of each link.

Compatibility and Considerations

It's important to note that the InAppBrowser plugin may behave slightly differently across platforms. On iOS, using the _system parameter opens links in Safari; on Android, it opens in the device's default browser. Additionally, some platforms might require extra configuration permissions.

Another significant consideration is user experience. When links open in the system browser, users need to manually return to the app. To improve this, consider using other InAppBrowser opening methods like _blank (opens a new window within the app) or _self (opens in the current WebView), but this requires more complex navigation control.

Finally, ensure testing is done on real devices or emulators, as some behaviors may not be accurately simulated in desktop browsers.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.