android - Cordova Phonegap 和 Google Maps v3 javascript api : How to add backbutton functionality when clicking either the license link or the Google maps logo

标签 android html google-maps cordova

背景: Android 上运行的 Cordova phonegap 2.2 应用程序允许监听后退按钮事件

document.addEventListener("backbutton", function(e){ history.back();}

google maps api V3 创建的 map 在左下角带有 Google Logo 链接到 Google map 网页,在右下角有一个可点击的许可链接。 Logo 或服务条款链接没有特定的 ID/选择器。

问题 单击其中一个链接时,网页将重定向到带有以下内容的 Google 网页:target:_blank,然后该网站将在与 Cordava 应用程序相同的窗口中打开,但后退按钮功能会丢失,因为该网页包含自己的 Javascript。

是否可以在加载网页时注入(inject)一些代码?

一种解决方案可能是在外部打开链接:

navigator.app.loadUrl([href here], { openExternal:true } );

但是又出现了缺少选择器的问题。

更新:在许可页面或 Google map 中单击按钮时,我在 logcat 中收到以下错误消息:11-13 16:20:30.500: E/Web Console(31508): Uncaught ReferenceError: cordova is not定义:1

最佳答案

.live() 已在 jQuery v1.9 中删除,并在 Zepto v1.0rc1 中被弃用,因此这里是 kvaale 答案的修订版本,应该适用于最新的框架。

此版本还使用 PhoneGap/Cordova 的 InAppBrowser 代码,使您能够在 InAppBrowser(使用“_blank”)或系统 Web 浏览器(使用“_system”)中打开链接。

function directUrlToExternalBrowser(urlPattern){
    var pattern = "a[href^='"+urlPattern+"']";      // detect all urls starting with urlPattern

    $(document).on('click', pattern, function(e){
        e.preventDefault();
        var ref = window.open($(pattern).attr("href"), '_system', '');      // '_system' will open the system web browser, '_blank' will open the InAppBrowser
    });
}

然后将以下代码放入您的$(document).ready() 函数中...

directUrlToExternalBrowser("http://maps.google.com/maps");
directUrlToExternalBrowser("http://www.google.com/intl");

如果您想检测所有“a href”链接(不仅仅是那些用于 Google map 的链接),请改用以下代码...

directUrlToExternalBrowser("http://");  
directUrlToExternalBrowser("https://");      

关于android - Cordova Phonegap 和 Google Maps v3 javascript api : How to add backbutton functionality when clicking either the license link or the Google maps logo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13355842/

相关文章:

jquery - 如何创建 <select> 占位符

html - 纯CSS设置动画进度百分比

javascript - Google map - 更改事件时全屏显示?

android - 无法执行 aapt(AbstractIndexedListIterator.next(AbstractIndexedListIterator.java :80) 处的 NoSuchElementException

java - 计算 JSONArray 中的字符串和 JSONObject 的数量

java - 使用带有 Android SDK 的 Graph API 将图片上传到 Facebook

android - Google Map V2 android 错误 Inflating class fragment 错误

java - 检查 DAY_OF_WEEK 是否在星期一和星期五之间

javascript - 增加文本框的值

javascript - 如何在谷歌地图上显示方向后隐藏其他标记