2011-12-20 136 views
1

我正在使用以下页面显示iframe中的url内容并根据输入值更改它。更改iframe url更改父窗口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,而不是装载在IFRAME ...

为前HTTP:// WWW。 coolmath.com/在iframe中加载,但浏览一些链接时会在父窗口中加载整个页面。

回答

2

从www.coolmath.com:

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

意味着activly逃逸被陷害的网站。

+0

但以下也没有自己或顶部做同样的事情。 http://www.woodlands-junior.kent.sch.uk/maths/ – ArK 2011-12-20 10:14:01

+1

@Paniyar你提供的链接*有*自己和'顶部'。代码是: '' – 2011-12-20 15:11:52

2

您可能会注意到,即使像这样的<iframe src='http://www.coolmath-games.com/0-fraction-splat/index.html'></iframe>结果也是如此。这是一种叫做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为“禁用”此帧克星。

+0

我不能告诉你我一直在寻找这个多久。另一个检查可能是'if(top!= self)'。我在寻找一些旧的代码,不断弹出我的'iframe'页面,但不知道是什么造成了它。谢谢。 – CaptainBli 2012-05-25 01:20:10