【问题标题】:changing iframe url changes parent window url更改 iframe url 更改父窗口 url
【发布时间】:2011-12-20 10:05:07
【问题描述】:

我正在使用以下页面在 iframe 中显示 url 的内容并根据输入值更改它。

<html>
<head>
<script type="text/javascript">
function changeUrl()
{
 var url=document.getElementById('browseurl').value;
 if(url!="")
 {
   if(url.indexOf("http")==-1)
   {
    url="http://"+url;
   }
   document.getElementById('browserWnd').src=url;
 }
}
</script>
</head>

<body>
 <div >
   <span>Url</span> 
   <input id="browseurl" name="browseurl" type='textbox' />
   <input id="browse" type='button' value="changeurl" onclick="changeUrl()" />
 </div>

<iframe id="browserWnd" src="http://www.coolmath.com/" height="700" width="625"></iframe>
</div>
</body>
</html>

我的问题是浏览 iframe 中加载的 url 的一些内页 更改父窗口 url 而不是在 iframe 中加载...

对于前 http://www.coolmath.com/ 在 iframe 中加载,但在浏览某些链接时会在父窗口中加载整个页面。

【问题讨论】:

    标签: javascript iframe


    【解决方案1】:

    来自 www.coolmath.com:

    if (window.self != window.top) ...
    

    意味着该网站主动逃避被陷害。

    【讨论】:

    • 但在没有 self 或 top 的情况下,following 也可以这样做。 woodlands-junior.kent.sch.uk/maths
    • @Paniyar 您提供的链接确实selftop。代码为:&lt;script language="JavaSCript"&gt;if (top.location != location) top.location.href = location.href;&lt;/script&gt;
    【解决方案2】:

    您可能会注意到,即使像 &lt;iframe src='http://www.coolmath-games.com/0-fraction-splat/index.html'&gt;&lt;/iframe&gt; 这样的东西也会产生相同的结果。这是一种称为frame-busting 的技术。它在他们页面的代码中:

    <script>
    <!-- Hide Script
     if (top.location != self.location) {
       top.location = self.location
     }
    //End Hide Script-->
    </script>
    

    如果您有一个服务器,它可以用HTTP/1.1 204 No Content 标头响应,您可以按照here 的描述“停用”这个帧破坏器。

    【讨论】:

    • 我无法告诉你我已经搜索了多久。另一个检查可能是if(top != self)。我正在查看一些旧代码,这些代码不断从我的iframe 中弹出页面,但不知道是什么原因造成的。谢谢。
    猜你喜欢
    • 2014-06-17
    • 2010-12-24
    • 2019-01-28
    • 2012-05-08
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多