设置父位置的目的是什么

what's the purpose to set the parent location?

本文关键字:是什么 位置 设置      更新时间:2023-09-26

我在页面中嵌入了一个iframe。为什么我们需要从 iframe 设置永久位置 Href,有什么原因吗?

self.parent.location.href = blah blah blah;

这通常是一种断帧技术。

通常是这样的:

if(self != top)top.location.href=someUrl;

由于可以在页面正文中放置 iframe 标记,因此您可以使用 top 来操作主窗口。看:

主页

<!--This is the main page-->
<html>
    <head>
        <script>
        alert(window.top.location.href);//The main page's URL
        alert(window.self.location.href);//The main page's URL
        alert(window.top.location.href);//The main page's URL
        </script>
    </head>
    <body>
        <iframe src="myFrame.html"></iframe>
    </body>
</html>

我的框架.html

<html>
    <head>
        <script>
        alert(window.location.href);//"myFrame.html"
        alert(window.self.location.href);//"myFrame.html"
        alert(window.top.location.href);//The main page's URL
        </script>
    </head>
    <body>
    </body>
</html>