shouldOverrideUrlLoading()不要求从IFRAME外部链接 [英] shouldOverrideUrlLoading() not called for external links from iframe

查看:1283
本文介绍了shouldOverrideUrlLoading()不要求从IFRAME外部链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有shouldOverrideUrlLoading问题()在等都是很好的讨论(我可能读他们大多),我仍然没有找到一个解决方案是什么,似乎我这样一个普遍的问题,我认为已经得到解决。

Although issues with shouldOverrideUrlLoading() are well discussed in SO (I probably read most of them), I still didn't find a solution to what seems to me such a generic problem, that I believe has been solved.

问题:如何停止shouldOverrideUrlLoading()对外部链接,例如 http://www.example.com ,为我的网页加载webView.loadDataWithBaseURL(),其中的baseUrl为file:/// ......?

The QUESTION: How to stop in shouldOverrideUrlLoading() for external links, e.g. "http://www.example.com", for pages I loaded with webView.loadDataWithBaseURL(), where baseUrl is "file:///..."?

在外部链接感动不叫我的shouldOverrideUrlLoading()重写方法(点击)。
<一href=\"http://stackoverflow.com/questions/23298290/webview-shouldoverrideurlloading-not-called-for-invalid-links\">Here它说:不能对基本URL来解决URL是在地板上(你不会得到任何回调对他们来说,既不shouldOverrideUrlLoading也不onPageStarted)下跌了。
Android开发者网站说这里说:如果通过调用加载的页面。 .. loadDataWithBaseURL(),那么您将收到shouldOverrideUrlLoading()回调这种类型的链接页面上
这是我的code:

My override method for shouldOverrideUrlLoading() is not called when external links are touched (clicked). Here it says " URLs that can't be resolved against the base URL are dropped on the floor (you won't get any callbacks for them, neither shouldOverrideUrlLoading nor onPageStarted)." Android developer site says here that "If you loaded the page by calling ... loadDataWithBaseURL(), then you will receive the shouldOverrideUrlLoading() callback for this type of link on the page." This is my code:

public void loadEpub(final EpubInfo epubInfo)
{
    post(new Runnable()
    {
        @Override
        public void run()
        {
            epubBaseUrl = "file://" + epubInfo.path;
            if (!epubBaseUrl.endsWith("/"))
                epubBaseUrl += "/";

            String path = epubBaseUrl + epubInfo.baseUrl;
            String page = generatePage(epubInfo);
            EpubWebView.super.loadDataWithBaseURL(path, page, "text/html", "UTF-8", null);
        }
    });
}

在我的的baseUrl(路径)。文件:///存储/ sdcard0 / Android的/数据/ ...

where my baseUrl (path) is "file:///storage/sdcard0/Android/data/...".

提前非常感谢。

推荐答案

明确指出问题,并找到了解决办法。
如果您的网页在 IFRAME运行,点击外部( HTTP:// WWW。 。)链接不会触发 shouldOverrideUrlLoading()!除非你加入目标=_顶
所以至少有2种可能的解决方案:
1)如果可以的话,如果没有运行 IFRAME
2)如果您添加到每个的href 目标=_顶 shouldOverrideUrlLoading() 将被触发。

Pinpointed the problem, and found a solution. If your page runs in an iframe, clicking on external (http://www...) links does NOT trigger shouldOverrideUrlLoading()! Unless you add target="_top" So there are at least 2 possible solutions: 1) if you can, run without an iframe 2) if you add to each href, target="_top", shouldOverrideUrlLoading() WILL BE triggered.

我去溶液2)。以下的JavaScript code增加了目标属性,每一个环节。

I went for solution 2). The following JavaScript code adds the target attribute to each link.

function change_links_target()
{   
    var all_document_links = mFrameDocument.getElementsByTagName("a");
    for (i = 0; i < all_document_links.length; i++){
        all_document_links[i].setAttribute("target", "_top");
    }
}   

这篇关于shouldOverrideUrlLoading()不要求从IFRAME外部链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆