阻止iframe中的链接导航/接管cordova webview,尤其是在ios中 [英] Prevent links in iframe to navigate/takeover cordova webview, especially in ios

查看:192
本文介绍了阻止iframe中的链接导航/接管cordova webview,尤其是在ios中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用iframe显示外部内容的Cordova应用程式(在这种情况下是YouTube嵌入的影片)



点击iframe中的连结(例如YouTube标志)整个webview导航到youtube网站,而不只是iframe。



在Android上,cordova插件api公开了onOverrideUrlLoading方法,它应该能够阻止不必要的导航。 (还没有自己测试)



我没有在ios cordova类上找到类似的方法,也没有在ios webview api中找到类似的方法。但我不是很熟悉ios。



我也试过了(在android& ios)iframe的沙箱属性
https://developer.mozilla.org/en/docs/Web/HTML/Element / iframe#attr-sandbox
这适用于防止脚本(不是我需要atm,只是为了检查属性是否正在做某事),但不是为了防止导航(启用导航,你应该添加

使用html5沙盒属性
,同时不允许允许allow-top-navigation和我没有)

解决方案

顶部导航& allow-popups

 < iframe sandbox =allow-scripts allow-same-origin... 

测试于:ios 8 / 7.1,android 4.4



我之前的检查已向沙箱值添加allow-popups

 < iframe sandbox =allow-scripts allow-same -origin allow-popups... 


I have Cordova application which displays external content with iframe (youtube embedded video in that case)

When clicking links inside the iframe (like the youtube logo), The whole webview is navigated to the youtube website, not just the iframe. (without ability to go back, you need to kill the app), the behaviour i want to prevent.

On android, the cordova plugin api exposes onOverrideUrlLoading method, which should be able to block unwanted navigations. (have't tested it myself yet)

I haven't found similar method on ios cordova classes, nor in ios webview api. but i'm not really familiar with ios.

I've also tried (on android & ios) iframe's sandbox attribute https://developer.mozilla.org/en/docs/Web/HTML/Element/iframe#attr-sandbox That works for preventing scripts (not that i need that atm, just to check if the attribute is doing something) but not to prevent the navigation (to enable navigation you should add allow-top-navigation and i didn't)

解决方案

Use html5 sandbox attribute, while not allowing allow-top-navigation & allow-popups

<iframe sandbox="allow-scripts allow-same-origin"...

Tested on: ios 8/7.1, android 4.4

In my previous check i've added allow-popups to the sandbox value

<iframe sandbox="allow-scripts allow-same-origin allow-popups"...

这篇关于阻止iframe中的链接导航/接管cordova webview,尤其是在ios中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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