javascript - android webview 从 Javascript 打开浏览器窗口

标签 javascript android iframe webview

我在 WebView 中添加了一个移动谷歌广告(在一个 fragment 中)。当您点击我想在新浏览器中打开的广告时。我启用了以下功能:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

google.js 正在生成一个 iFrame,但是点击 webview 似乎没有注册为新的链接点击,也没有调用 shouldOverrideUrlLoading

最佳答案

在另一个so问题中找到答案here ,

但是我修改了它以适用于 fragment (包括 getActivity())。 请注意,这是专门针对谷歌广告的。在您的 webclient 中覆盖以下方法。

  @Override
        public void onLoadResource(WebView view, String url)
        {
            // the url that contains the ad to link to contains googleads, this is risky if google
            // change it, but there couldn't find a nice way around
            if (url.contains("googleads"))
            {
                HitTestResult hitTestResult = view.getHitTestResult();
                // Check is the hit test is a src tag with image anchor (i.e an mobile ad) 
                if (hitTestResult.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE)
                {
                    getActivity().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    view.stopLoading();                        
                }
            }
        }

关于javascript - android webview 从 Javascript 打开浏览器窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9115009/

相关文章:

javascript - 如何将 $q 传递给 Angular Directive(指令)链接函数?

Android - 如何设置 ListView 的最大大小

javascript - 在 iframe 中隐藏 div

javascript - 第一次在主页上自动放映幻灯片

javascript - 使用js验证表单

javascript - 在 Google Maps V3 中获取 GeoJSON 数据层的属性

android - 是否可以获取共享对象的版本?

android - 如何找到哪些 android 设备属于 xdpi、mdpi、Idpi、xxhdpi、xhdpi

c# - 使用 XPath 和 C# 移动多个 iFrame

javascript - 具有良好 iFrame 支持的 Vanilla JS Modal