防止IFrame导航 [英] Prevent IFrame Navigation
问题描述
我使用 iframe
通过JS将HTML字符串写入内容文档来显示HTML电子邮件内容。如果他点击显示的电子邮件中的链接,我不希望用户在 iframe
内导航。现在我有以下选项:
I use an iframe
to display HTML email contents by writing a HTML string to it's content document via JS. I don't want the user to navigate inside that iframe
if he clicks a link in the displayed email. Now I have these options:
- 在新的浏览器窗口或标签页中打开链接(首选)
- 防止所有导航(次优,只有在没有其他选项的情况下)
- 我不知道更好的选择?
我怎样才能完成这两个选项中的任何一个?
How could I accomplish any of those 2 options?
我知道 HTML5
引入了沙箱
属性,但不幸的是,这只适用于 Chrome
和 Safari
,所以这不是一个选项。
I know that HTML5
introduced a sandbox
property, but unfortunately that only works in Chrome
and Safari
, so that's not an option.
推荐答案
你可以遍历所有的链接iframe并向他们添加 target =_ blank
。
You could loop through all the links in the iframe and add target="_blank"
to them.
(对不起,我只知道如何做到这一点在JQuery中很抱歉链接这个例子没有在问题中标记):
(Excuse me but I only know how to do this in JQuery so sorry for linking the example in this without it being tagged in the question):
$("#iframeID").contents().find("a").each(function() {
$(this).attr("target", "_blank");
});
示例: http://jsfiddle.net/yP7Nd/ - (忽略我的JQuery代码上面的JS,我必须这样做才能正确加载一些IFrame内容)。
Example: http://jsfiddle.net/yP7Nd/ - (ignore the bit of JS above my JQuery code, I had to do this to get some IFrame content to load properly).
这篇关于防止IFrame导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!