0

我有一个 iframe 显示来自 html 页面(在同一域内)的内容,假设 iframe 内容是mydomain.com/page1.html.

显然,我想要page1.html在 iframe 中显示的内容。

但是,当在浏览器中键入整个 URL () 时,我希望将此文件 ( page1.html) 重定向到我的主站点 ( ),而不在 iframe 中更改它。mydomain.commydomain.com/page1.html

这甚至是可行的吗?我将不胜感激。谢谢。

4

1 回答 1

0

是的,从您所写的内容来看,您似乎希望发生这种情况:

  • 输入浏览器的 mydomain.com/page1.html 重定向到 mydomain.com/

  • 加载到 iframe 中的 mydomain.com/page1.html 不会重定向,但会保持在 iframe 中不变

您可以添加这个非常简单的脚本page1.html来完成此操作:

<script>
if (top == self)
    location.href = '/';
</script>

警告:不要将此脚本添加到您域的默认主页,否则您将导致无限加载循环。

自包含示例

主页index.html包含:

<p>This is the home page, containing a frame.</p>
<iframe src="page1.html"></iframe>

框架页面page1.html包含:

<script>
if (top == self)
    location.href = '/';
</script>
<p>
This page will display in a frame only.
Loading it directly as the main page will redirect
the user to the default home page of the
website.
</p>

结果

加载网站的主域将显示文本“这是主页,包含一个框架”。以及框架,其中包含“此页面将仅显示在框架中......”

直接进入网站的主域然后/page1.html会立即重定向回主域的主页本身。

于 2013-07-31T21:10:38.123 回答