保留iframe内容以显示源html,但要重定向源页面

保留iframe内容以显示源html,但要重定向源页面,html,redirect,iframe,Html,Redirect,Iframe,我有一个iframe,它显示html页面中的内容(在同一个域中),假设iframe内容是mydomain.com/page1.html 显然,我希望在iframe中显示page1.html的内容 但是,当在浏览器中键入整个URL(mydomain.com/page1.html)时,我希望将此文件(page1.html)重定向到我的主站点(mydomain.com),而不在iframe中更改它 这是可行的吗?我将感谢任何帮助。谢谢。是的,从你写的内容来看,听起来你希望这样: 在浏览器中键入myd

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

显然,我希望在iframe中显示
page1.html
的内容

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


这是可行的吗?我将感谢任何帮助。谢谢。

是的,从你写的内容来看,听起来你希望这样:

  • 在浏览器中键入mydomain.com/page1.html会重定向到mydomain.com/

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

您可以将这个非常简单的脚本添加到
page1.html
,以完成以下任务:

<script>
if (top == self)
    location.href = '/';
</script>
框架页
page1.html
包含:

<p>This is the home page, containing a frame.</p>
<iframe src="page1.html"></iframe>
<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>

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

此页面将仅显示在框架中。
直接将其加载为主页面将重定向
将用户添加到的默认主页
网站。

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


直接进入网站的主域名,后跟
/page1.html
,将立即重定向回主域名的主页。

我想你正在寻找这样的信息:@alfasin我想他可能也有类似的意思,但再次阅读问题,我相信OP只是想阻止该框架作为网站的孤立部分被访问,如果以这种方式访问,则重定向回完整的主页。