如何强制 iFrame 打开的所有链接都保留在 iframe 中?

4

我正在托管一个iFrame,当链接指向外部域时,它似乎会在主窗口中加载而不是在iFrame中加载。是否有一种方法可以强制链接在同一个iFrame中打开?

注意:我可以通过使用Chrome扩展程序添加任何我想要的内容到加载在iFrame中的页面。

我尝试添加:

<base target="_parent" />

但是这并没有起到任何作用...

1个回答

5
检查您的外部域页面的标头。如果X-Frame-Options标头设置为DENY,浏览器将忽略加载内容到iframeframe set中,并在浏览器的主框架中加载。
Regardless of the site attempts the page cannot be displayed in a frame.

你的代码

<base target="_parent" /> 用于将结果加载到当前浏览上下文的父级中。如果没有父级,这个选项会像_self一样运行,因此它无法解决你的问题。

使用 <base target="myframe">代替其中的myframe是你iframe的名称。

<iframe width="200" height="200" name="myframe"></iframe>

演示

查找服务器头以加载页面

<html>

    <head>
        <base target="myframe">
    </head>

    <body>
        <a href="hi.html">Base</a>
        <a href="bye.html">A Tag</a>
        <iframe width="200" height="200" name="myframe"></iframe>
    </body>

</html>

References


1
我不确定那会怎么帮助。我已经加载了一个iFrame,点击iFrame中的链接会导致页面在主框架和内部iFrame中加载。 - Guy Korland
非常感谢您的回答:“使用<base target="myframe">,其中myframe是您的iframe的名称。”正如您所期望的那样完美地工作。我不知道您可以将元素用作目标。 - Justin Hanley

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接