PhoneGap: Open external link in default browser (outside the app)

前端 未结 16 1786
我在风中等你
我在风中等你 2020-12-09 15:36

I\'m trying to open links in Safari (on an iPhone) from a PhoneGap application. I\'m using PhoneGap version 3.1.0, and use PhoneGap Build, to build the application.

16条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 16:09

    I adapted pastullo's answer to work also in a WebApp that might be opened in a cordova InAppBrowser, but also als plain Web-App (e.g. for testing):

    function initOpenURLExternally() {
        $("a[target='_blank'],a[target='_system']").each(function (i) {
            var $this = $(this);
            var href = $this.attr('href');
            if (href !== "#") {
                $this
                    .attr("onclick", "openURLExternally(\"" + href + "\"); return false;")
                    .attr("href", "#");
            }
        });
    }
    
    function openURLExternally(url) {
        // if cordova is bundeled with the app (remote injection plugin)
        log.trace("openURLExternally: ", url);
        if (typeof cordova === "object") {
            log.trace("cordova exists ... " + typeof cordova.InAppBrowser);
            if (typeof cordova.InAppBrowser === "object") {
                log.trace("InAppBrowser exists");
                cordova.InAppBrowser.open(url, "_system");
                return;
            }
        }
    
        // fallback if no cordova is around us:
        //window.open(url, '_system', '');
        window.open(url, '_blank', '');
    }
    

提交回复
热议问题