设置父位置的目的是什么?

时间:2012-06-10 10:39:07

标签: javascript html5

我在页面中嵌入了iframe。为什么我们需要从iframe设置perent位置Href,原因是什么?

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

2 个答案:

答案 0 :(得分:1)

这通常是一种断帧技术。

通常是这样的:

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

答案 1 :(得分:0)

由于可以在页面的正文中放置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>

<强> myFrame.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>