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

前端 未结 16 1704
我在风中等你
我在风中等你 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:30

    This is how i solved in Cordova/Phonegap 3.6.3

    Install the inappbroswer cordova plugin:

    cordova plugin add org.apache.cordova.inappbrowser
    

    I wanted to keep my phonegap app as similar as possible to a standard web page: I wanted that by having target="_blank" on a link, it would open in an external page.

    This is how i implemented it:

    $("a[target='_blank']").click(function(e){
      e.preventDefault();
      window.open($(e.currentTarget).attr('href'), '_system', '');
    });
    

    so all i have to do is use a link like the following

    <a href="http://www.example.com" target="_blank">Link</a>
    
    0 讨论(0)
  • 2020-12-09 16:30

    Late answer but maybe it will be helpful for someone. So what I have in my working code in both iOS and Android application based on Cordova. To open external link in the default browser (Safari or Google):

    1) Make sure that you have inAppBrowser plugin

    cordova plugin add cordova-plugin-inappbrowser --save
    

    2) Add to device.js

    function openURL(urlString){
      let url = encodeURI(urlString);
      window.open(url, '_system', 'location=yes');
    }
    

    3) Create new link

    <a href="#" onClick="openURL('http://www.google.com')">Go to Google</a>
    
    0 讨论(0)
  • 2020-12-09 16:30

    For those that didn't spot it in the original question you also need to make sure the URL you are trying to open isn't whitelisted by adding an access tag to your config.xml as follows:

    <access origin="http://www.example.com" />
    

    or you can do

    <access origin="*" />
    

    to allow anything

    0 讨论(0)
  • 2020-12-09 16:32

    try with this below example.

    <a class="appopen" onClick="OpenLink();">Sign in</a>
    
    
    <script>
    function OpenLink(){
        window.open("http://www.google.com/", "_system");
    }
    </script>
    

    add this Below line in config.xml if you are using PhoneGap Version 3.1 or above

    <gap:plugin name="org.apache.cordova.inappbrowser" />
    
    0 讨论(0)
提交回复
热议问题