Cordova, why would InAppBrowser plugin be required to open links in system browser

Sebastien Lorber

I have a Cordova app, it is a single page application with a single HTML file.

All links should open in the system browser. I don't want an "embedded" InAppBrowser but really the native system / external browser.

Everywhere we can find example of code using InAppBrowser with something like:

window.open('http://apache.org', '_system');

But why do we need to install InAppBrowser, even if we don't even plan to use an embedded browser?

Can someone really expain what is supposed to be the behavior of a WebView, regarding the target of a link. It is not clear what it is supposed to do with a target=_blank, but I don't see anything else it can do except opening a new browser window.

Notice that the problem seems to only be with iOS because with Android (with Crosswalk plugin) using target=_blank seems to always work fine and open in a new native browser window.

Sebastien Lorber

So I'm answering my own question with what I've found out. Note I'm only dealing with iOS and Android (with Crosswalk plugin) on Cordova 5.1.1, and it may not apply to other platforms/versions.

InAppBrowser is required

Even if you don't need an embedded browser, InAppBrowser plugin is required. This makes the _system target available that triggers native plugin code to open the system/external browser.

So it seems the plugin is somehow a "2 in 1" plugin: it permits to use an embedded browser + it permits to securely force the external system browser to open.

It is not clear what the default WebView behavior should be relative to _blank links (nor if it is standardized in any way for WebViews) but I've found no way to open the external browser on iOS without this plugin or native code.

Opening _self in WebView, and _blank in native browser

If like me you don't care about the embedded browser, but just want to open all _blank targets to the native external browser in an existing app, without too much pain (particularly if the app is also a mobile website...), you can run the following code at the beginning of your app:

function openAllLinksWithBlankTargetInSystemBrowser() {
    if ( typeof cordova === "undefined" || !cordova.InAppBrowser ) {
        throw new Error("You are trying to run this code for a non-cordova project, " +
                "or did not install the cordova InAppBrowser plugin");
    }

    // Currently (for retrocompatibility reasons) the plugin automagically wrap window.open
    // We don't want the plugin to always be run: we want to call it explicitly when needed
    // See https://issues.apache.org/jira/browse/CB-9573
    delete window.open; // scary, but it just sets back to the default window.open behavior
    var windowOpen = window.open; // Yes it is not deleted !

    // Note it does not take a target!
    var systemOpen = function(url, options) {
        // Do not use window.open becaus the InAppBrowser open will not proxy window.open
        // in the future versions of the plugin (see doc) so it is safer to call InAppBrowser.open directly
        cordova.InAppBrowser.open(url,"_system",options);
    };


    // Handle direct calls like window.open("url","_blank")
    window.open = function(url,target,options) {
        if ( target == "_blank" ) systemOpen(url,options);
        else windowOpen(url,target,options);
    };

    // Handle html links like <a href="url" target="_blank">
    // See https://issues.apache.org/jira/browse/CB-6747
    $(document).on('click', 'a[target=_blank]', function(event) {
        event.preventDefault();
        systemOpen($(this).attr('href'));
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

cordova-plugin-inappbrowser not opening links from APP to IOS Device default Browser

From Dev

Cordova plugin InAppBrowser does not open a second time

From Dev

How do I gracefully interrupt loading of a certain URL in Cordova's InAppBrowser and open it in the system browser?

From Dev

Cordova InAppBrowser plugin is not installed

From Dev

Angular Marked and Inappbrowser opening all links in the system browser

From Dev

Angular Marked and Inappbrowser opening all links in the system browser

From Dev

Cordova InAppBrowser Plugin - Windows 10

From Dev

Open Intel XDK links in system browser?

From Dev

Cordova InAppBrowser.open. Links are opening on safari instead of opening inside the app

From Dev

Xamarin Forms - Equivalent of cordova-plugin-inappbrowser?

From Dev

Cordova/PhoneGap Open Downloaded File (InAppBrowser)

From Dev

Open Camera or Gallery from " inappbrowser " in Apache Cordova

From Dev

Cordova/PhoneGap Open Downloaded File (InAppBrowser)

From Dev

Open all links using system browser inside an ionic app

From Dev

file:// links open in non-default browser on Windows. Why?

From Dev

Why is internet explorer the only browser that can open .url links by default?

From Dev

External links / InAppBrowser in Cordova / Phonegap 3.3 always behave like "_self"

From Dev

Why is the Cordova Browser-Sync Plugin not working on a clean, new Apache Cordova app?

From Dev

Ionic 2 cordova InAppBrowser browser.close() is not a function

From Dev

Error installing cordova-plugin-inappbrowser: "Error: Expected "*/" or [^*] but "*" found"

From Dev

Error installing cordova-plugin-inappbrowser: "Error: Expected "*/" or [^*] but "*" found"

From Dev

External links don't open in the inAppBrowser on iOS on phonegap 2.9.0

From Dev

Ionic Cordova InAppBrowser doesn't open the url a second time

From Dev

Why the repaint in browser would be defered?

From Dev

Cordova adding port to external links on cordova run browser

From Dev

When middle-clicking links, why does my browser sometimes open two tabs?

From Dev

Links (and clicks) fail to open with Cordova on iOS 8.4.1

From Dev

Object Required Error - Why would this be out of scope?

From Dev

problems with InAppBrowser on dynamic links

Related Related

  1. 1

    cordova-plugin-inappbrowser not opening links from APP to IOS Device default Browser

  2. 2

    Cordova plugin InAppBrowser does not open a second time

  3. 3

    How do I gracefully interrupt loading of a certain URL in Cordova's InAppBrowser and open it in the system browser?

  4. 4

    Cordova InAppBrowser plugin is not installed

  5. 5

    Angular Marked and Inappbrowser opening all links in the system browser

  6. 6

    Angular Marked and Inappbrowser opening all links in the system browser

  7. 7

    Cordova InAppBrowser Plugin - Windows 10

  8. 8

    Open Intel XDK links in system browser?

  9. 9

    Cordova InAppBrowser.open. Links are opening on safari instead of opening inside the app

  10. 10

    Xamarin Forms - Equivalent of cordova-plugin-inappbrowser?

  11. 11

    Cordova/PhoneGap Open Downloaded File (InAppBrowser)

  12. 12

    Open Camera or Gallery from " inappbrowser " in Apache Cordova

  13. 13

    Cordova/PhoneGap Open Downloaded File (InAppBrowser)

  14. 14

    Open all links using system browser inside an ionic app

  15. 15

    file:// links open in non-default browser on Windows. Why?

  16. 16

    Why is internet explorer the only browser that can open .url links by default?

  17. 17

    External links / InAppBrowser in Cordova / Phonegap 3.3 always behave like "_self"

  18. 18

    Why is the Cordova Browser-Sync Plugin not working on a clean, new Apache Cordova app?

  19. 19

    Ionic 2 cordova InAppBrowser browser.close() is not a function

  20. 20

    Error installing cordova-plugin-inappbrowser: "Error: Expected "*/" or [^*] but "*" found"

  21. 21

    Error installing cordova-plugin-inappbrowser: "Error: Expected "*/" or [^*] but "*" found"

  22. 22

    External links don't open in the inAppBrowser on iOS on phonegap 2.9.0

  23. 23

    Ionic Cordova InAppBrowser doesn't open the url a second time

  24. 24

    Why the repaint in browser would be defered?

  25. 25

    Cordova adding port to external links on cordova run browser

  26. 26

    When middle-clicking links, why does my browser sometimes open two tabs?

  27. 27

    Links (and clicks) fail to open with Cordova on iOS 8.4.1

  28. 28

    Object Required Error - Why would this be out of scope?

  29. 29

    problems with InAppBrowser on dynamic links

HotTag

Archive