shouldOverrideUrlLoading() 不为来自 iframe 的外部链接调用
shouldOverrideUrlLoading() not called for external links from iframe
虽然 shouldOverrideUrlLoading() 的问题在 SO 中得到了很好的讨论(我可能阅读了其中的大部分内容),但我仍然没有找到在我看来如此普遍的问题的解决方案,我相信已经解决了。
问题:如何在 shouldOverrideUrlLoading() 中停止外部链接,例如“http://www.example.com”,对于我使用 webView.loadDataWithBaseURL() 加载的页面,其中 baseUrl 是 "file:///..."?
当触摸(单击)外部链接时,我的 shouldOverrideUrlLoading() 覆盖方法没有被调用。
Here 它说“URL 无法根据基础 URL 解决的问题被丢弃在地板上(你不会得到任何回调,无论是 shouldOverrideUrlLoading 还是 onPageStarted)。 “
Android 开发者网站说 here "If you loaded the page by calling ... loadDataWithBaseURL(), then you will receive the shouldOverrideUrlLoading() callback for this type of link on the page."
这是我的代码:
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(路径)是 "file:///storage/sdcard0/Android/data/..."。
非常感谢。
查明了问题,并找到了解决办法。
如果您的页面 运行s 在 iframe
中,点击外部 (http://www...) links 不会触发 shouldOverrideUrlLoading()
!除非你加上 target="_top"
所以至少有两种可能的解决方案:
1) 如果可以,运行 没有 iframe
2) 如果您添加到每个 href
,target="_top"
,shouldOverrideUrlLoading()
将被触发。
我选择了解决方案 2)。以下 JavaScript
代码将目标属性添加到每个 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() 的问题在 SO 中得到了很好的讨论(我可能阅读了其中的大部分内容),但我仍然没有找到在我看来如此普遍的问题的解决方案,我相信已经解决了。
问题:如何在 shouldOverrideUrlLoading() 中停止外部链接,例如“http://www.example.com”,对于我使用 webView.loadDataWithBaseURL() 加载的页面,其中 baseUrl 是 "file:///..."?
当触摸(单击)外部链接时,我的 shouldOverrideUrlLoading() 覆盖方法没有被调用。 Here 它说“URL 无法根据基础 URL 解决的问题被丢弃在地板上(你不会得到任何回调,无论是 shouldOverrideUrlLoading 还是 onPageStarted)。 “ Android 开发者网站说 here "If you loaded the page by calling ... loadDataWithBaseURL(), then you will receive the shouldOverrideUrlLoading() callback for this type of link on the page." 这是我的代码:
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(路径)是 "file:///storage/sdcard0/Android/data/..."。
非常感谢。
查明了问题,并找到了解决办法。
如果您的页面 运行s 在 iframe
中,点击外部 (http://www...) links 不会触发 shouldOverrideUrlLoading()
!除非你加上 target="_top"
所以至少有两种可能的解决方案:
1) 如果可以,运行 没有 iframe
2) 如果您添加到每个 href
,target="_top"
,shouldOverrideUrlLoading()
将被触发。
我选择了解决方案 2)。以下 JavaScript
代码将目标属性添加到每个 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");
}
}