PhoneGap和Cordova无法在应用程序浏览器中打开链接

3
我是一个新手,正在尝试为我的网站创建一个包装应用程序脚本。我需要使用PhoneGap和Cordova进行开发。
然而,我遇到的问题是,在使用PhoneGap云构建应用程序并单击页面中的外部链接时,它会在Chrome中打开链接而不是原生浏览器。我需要安装什么东西吗?像插件之类的东西?
我已经检查了PhoneGap桌面应用程序为我创建的项目中是否有插件,但是我需要将它们复制到www文件夹中吗?
/plugins/cordova-plugin-inappbrowser

我的代码很简单,我正在尝试几种不同的方法在本机浏览器中打开它:

<div id="deviceready" class="blink">
    <h1>Test App</h1>
    <a href='#' onclick='navigator.app.loadUrl("http://www.tutorialspoint.com/", {openExternal : false});'/>Test</a><br/>
    <a href='#' onclick="navigator.app.loadUrl('http://www.tutorialspoint.com/', { openExternal:true });">Link</a><br/>
    <a href="#" onclick="window.open('http://www.tutorialspoint.com/', '_system');">system</a><br/>
    <a href='http://www.tutorialspoint.com/'/>Google</a><br/>
    <a href='newpage.html'/>newpage</a><br/>
</div>

检查白名单插件https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-whitelist/ - abdoutelb
@AbdouTelb 谢谢您的回复,我已经完成了。 - ffmsingh
1个回答

5

首先,您需要像这样初始化插件:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    window.open = cordova.InAppBrowser.open;
}

这是来自https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-inappbrowser/的内容:

_blank:在InAppBrowser中打开。

_system:在系统的网络浏览器中打开。

因此,您需要更改:
<a href="#" onclick="window.open('http://www.tutorialspoint.com/', '_system');">system</a><br/>

转化为:

<a href="#" onclick="window.open('http://www.tutorialspoint.com/', '_blank', 'location=yes');">system</a><br/>

嗨Jesper,谢谢你的评论,我刚刚尝试了你的建议,但它仍然在Chrome中打开链接。 - ffmsingh
@ffmsingh - 你有初始化InAppBrowser吗?请查看更新的解决方案。 - Jesper Højer
@ffmsingh - 我需要看看你的 JavaScript 代码。 - Jesper Højer
谢谢您的回复,我已经解决了这个问题 :) 我把我的JavaScript放错位置了!我把它放在index.js的底部,但当我把它移到index.html的底部时它可以工作。 - ffmsingh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接