java 如何捕获 iframe 重定向?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14990573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:06:54  来源:igfitidea点击:

How to catch an iframe redirecting?

javaandroidwebview

提问by Gábor

I have a WebView with an iframe inside. The contents of the iframe do a redirect to another URL that doesn't allow embedding in an iframe. Not a problem, actually, I just want to start the external browser activity in this case instead. The problem is that I can't find a way to intercept the redirection. None of shouldOverrideUrlLoading(), onPageStarted() or onLoadResource() get called for the redirect, hence I get no chance to start the activity.

我有一个带有 iframe 的 WebView。iframe 的内容会重定向到另一个不允许嵌入 iframe 的 URL。没问题,实际上,我只想在这种情况下启动外部浏览器活动。问题是我找不到拦截重定向的方法。shouldOverrideUrlLoading()、onPageStarted() 或 onLoadResource() 都没有被调用以进行重定向,因此我没有机会开始活动。

回答by Ben Roby G

In the html change the iframe tag to call some javascript function when a redirect happens

在 html 中更改 iframe 标记以在发生重定向时调用一些 javascript 函数

example: iFrame src change event detection?

示例:iFrame src 更改事件检测?

回答by Myles Bennett

For anyone else with this problem, we found an even more simple solution to this than calling a javascript function. Simply add "target= _top" to the iframe redirect (see http://www.w3schools.com/tags/att_a_target.asp). Then shouldOverrideUrlLoading() will be called in the Android code.

对于遇到此问题的其他任何人,我们找到了比调用 javascript 函数更简单的解决方案。只需将“target= _top”添加到 iframe 重定向(请参阅http://www.w3schools.com/tags/att_a_target.asp)。然后应该在Android 代码中调用shouldOverrideUrlLoading()。