【发布时间】:2014-07-01 09:03:36
【问题描述】:
我正试图强制我的应用程序中的链接在外部浏览器中打开。我正在使用 jQuery mobile 和 Phonegap 构建。
我的应用有一个页面,其中包含生成的联系方式。在详细信息中,有一个指向此人网站的链接:
row+='<div class="accWWW"> <a href="http://'+ data[i].website +'" onclick="openGenerated(this); return false;">'+ data[i].website +'</a></div>';
我的代码中定义了这两个函数。
//function which forces open in browser
function openBrowser(url){
if(device.platform === 'Android') {
navigator.app.loadUrl(url, {openExternal:true});
} else {
window.open(url, '_system');
}
}
//opens generated URL's
function openGenerated(obj){
var url = obj.getAttribute("href");
console.log(url);
openBrowser(url);
return false;
}
我的 index.html 中还有一个简单的 <div class="accWWW"> <a href="#" onclick="openBrowser('http://www.google.com');"> Google </a></div> 设置
所以...这个指向 Google 的链接在 iOS 中完美打开(尚未在 Android 上测试,因为 iOS 是我最大的问题),但如果我没有添加 openGenerated 函数,生成的链接将不会做任何事情。现在有一个动作,但它在应用程序内打开。
你知道这是为什么吗?
此外,在检查笔记本电脑时,我在控制台中收到这些错误(link 是我的开发网址):
Uncaught ReferenceError: device is not defined *link*:277 // makes sense probably, because I am not using a mobile device
Uncaught SecurityError: Blocked a frame with origin "http://www.test.com" from accessing a frame with origin "*link*". Protocols, domains, and ports must match.// what is this?
我在想可能是访问问题,但我在 config.xml 中添加了<access origin="*" browserOnly="true" />,所以这应该没有问题。
稍后编辑:在 Android 上,Google 链接根本不起作用,生成的链接在应用程序内打开...显然 device.platform 始终为空,但设备插件已添加到我在 phonegap 中看到的 xml 构建这个插件列表:
Installed Plugins
Apps will be built with the latest version of a plugin unless you lock the version in your config.xml (see below). -- Plugin installed as a dependency of another plugin
Third Party VERSION LATEST VERSION
com.phonegap.plugin.statusbar 1.1.0 1.1.0
PhoneGap Core
org.apache.cordova.device 0.2.8 0.2.8
org.apache.cordova.inappbrowser 0.3.3 0.3.3
稍后编辑:
Device.platform 似乎没有工作,所以我修改了我的函数,添加了 Ved 的建议:
函数 openBrowser(url){
if(navigator.app)
navigator.app.loadUrl(url, {openExternal:true});
else {
var ref = window.open(url, '_system', 'location=no');
ref.addEventListener('loadstart', function(event) {
});
ref.addEventListener('loadstop', function(event) {
console.log(event.type + ' - ' + event.url);
});
ref.addEventListener('exit', function(event) {
});
}
}
在 iOS 上仍会在应用内打开。对于 Android,我认为它运行良好。
【问题讨论】:
-
首先需要安装InAppBrowser插件按照这个链接代码。 stackoverflow.com/questions/21729288/…
-
仍然在应用程序内打开。我的函数现在看起来就像我上次编辑时一样。
-
window.open(your_url, '_system');
-
是的,我也尝试过。不起作用...我确实尝试了我在 Google 上找到的所有内容。
-
首先删除所有代码并仅运行此代码...可能有冲突。
标签: android jquery ios phonegap-build inappbrowser