PhoneGap:在默认浏览器中打开外部链接(在应用程序外部)

Mar*_*tin 26 phonegap-plugins cordova phonegap-build inappbrowser

我正试图通过PhoneGap应用程序在Safari(在iPhone上)打开链接.我正在使用PhoneGap 3.1.0版,并使用PhoneGap Build来构建应用程序.

我在页面上有两个链接(如下面的www/index.html所示).两个链接都在PhoneGap应用程序内打开.我可以看到PhoneGap已正确加载,因为警报('设备就绪!'); 被触发了.

我需要更改什么,在默认浏览器中打开链接(在应用程序外部)?

WWW/config.xml的文件看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>

<widget xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0" id="com.company.appname" version="0.0.3">
  <name>AppName</name>
  <description>description</description>
  <author href="http://www.example.com/" email="hello@example.com">
    Company
  </author>
  <preference name="phonegap-version" value="3.1.0" />
  <preference name="orientation" value="portrait" />
  <preference name="stay-in-webview" value="false" />

  <gap:plugin name="org.apache.cordova.inappbrowser" version="0.2.3" />
  <gap:plugin name="org.apache.cordova.dialogs" version="0.2.2" />
  <gap:plugin name="com.phonegap.plugins.pushplugin" version="2.0.5" />

  <plugins>
    <plugin name="InAppBrowser" value="CDVInAppBrowser" />
  </plugins>

  <feature name="InAppBrowser">
    <param name="ios-package" value="CDVInAppBrowser" />
  </feature>
  <access origin="*" />
</widget>
Run Code Online (Sandbox Code Playgroud)

WWW/index.html的文件看起来像这样:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <title>Test application</title>
</head>
<body>
  <a href="#" onclick="openUrl('http://www.google.com/'); return false;">Link test 1</a>
  <a href="#" onclick="window.open('http://www.google.com/', '_system', 'location=yes'); return false;">Test link 2</a>

  <script src="phonegap.js"></script>
  <script src="cordova.js"></script>
  <script src="js/jquery-1.9.1.js"></script>
  <script type="text/javascript">
    function openUrl(url) {
      alert("open url: " + url);
      window.open(url, '_blank', 'location=yes');
    }

    function onDeviceReady() {
      alert('device ready!');
    }
    $(function() {
        document.addEventListener("deviceready", onDeviceReady, true);
    });
  </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是项目结构:

??? platforms
??? plugins
?   ??? org.apache.cordova.inappbrowser
?       ??? LICENSE
?       ??? package.json
?       ??? plugin.xml
?       ??? README.md
?       ??? RELEASENOTES.md
?       ??? src
?       ?   ??? android
?       ?   ?   ??? InAppBrowser.java
?       ?   ?   ??? InAppChromeClient.java
?       ?   ??? blackberry10
?       ?   ?   ??? README.md
?       ?   ??? ios
?       ?   ?   ??? CDVInAppBrowser.h
?       ?   ?   ??? CDVInAppBrowser.m
?       ?   ??? wp
?       ?       ??? InAppBrowser.cs
?       ??? www
?           ??? InAppBrowser.js
?           ??? windows8
?               ??? InAppBrowserProxy.js
??? README.md
??? www
    ??? config.xml
    ??? cordova.js
    ??? index.html
    ??? js
    ?   ??? jquery-1.9.1.js
    ??? phonegap.js
Run Code Online (Sandbox Code Playgroud)

pas*_*llo 16

这就是我在Cordova/Phonegap 3.6.3中解决的问题

安装inappbroswer cordova插件:

cordova plugin add org.apache.cordova.inappbrowser
Run Code Online (Sandbox Code Playgroud)

我想保持我的phonegap应用程序尽可能与标准网页相似:我希望通过在链接上使用target ="_ blank",它将在外部页面中打开.

这就是我实现它的方式:

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

所以我要做的就是使用如下链接

<a href="http://www.example.com" target="_blank">Link</a>
Run Code Online (Sandbox Code Playgroud)


Red*_*678 10

这个怎么样?

<a href="#" onclick="window.open(encodeURI('http://www.google.com/'), '_system')">Test link 2</a>
Run Code Online (Sandbox Code Playgroud)

编辑:

这可能属于: 如何在onclick事件中调用多个JavaScript函数?

我在想,怎么样:

添加到代码:

$(".navLink").on('tap', function (e) {
    //Prevents Default Behavior 
    e.preventDefault();
    // Calls Your Function with the URL from the custom data attribute 
    openUrl($(this).data('url'), '_system');
});
Run Code Online (Sandbox Code Playgroud)

然后:

<a href="#" class="navLink" data-url="http://www.google.com/">Go to Google</a>
Run Code Online (Sandbox Code Playgroud)


wil*_*ast 9

您应该使用gap:plugin标记和config.xml中的完全限定ID来安装插件:

<gap:plugin name="org.apache.cordova.inappbrowser" />
Run Code Online (Sandbox Code Playgroud)

如此处所述.


Tab*_*ish 7

我使用的是cordova 6.0,这是我的解决方案:

1:安装此cordova插件.

cordova plugin add cordova-plugin-inappbrowser
Run Code Online (Sandbox Code Playgroud)

2:在html中添加开放链接,如下所示.

<a href="#" onclick="window.open('https://www.google.com/', '_system', 'location=yes');" >Google</a>
Run Code Online (Sandbox Code Playgroud)

3:这是最重要的步骤,因为我遇到了很多问题:下载cordova.js文件并将其粘贴到www文件夹中.然后在index.html文件中引用它.

<script src="cordova.js"></script>
Run Code Online (Sandbox Code Playgroud)

此解决方案适用于Android和iPhone环境.