2016-04-15 115 views
0

我有一种情况,我创建了一个网站,客户不希望其他开发人员能够使用iframe,但是当有人使用iframe时,它会将用户重定向到iframe试图通过的网站。从Iframe内部加载站点时重定向页面?

我一直在寻找代码,这将允许我检测客户端站点是否加载到iframe中,然后将其重定向回该站点。对于JS现场相当新颖,我不能100%确定如何去做这件事。

到目前为止,我有将被放入的document.ready功能

var url = "http://google.com"; 
$(location).attr('href',url); 

所以,现在的问题进来是利用通过检测另一个开发人员在一个iframe使用我的网站重定向的重定向代码在另一个网站上。

在此先感谢!

+0

https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options – Prinzhorn

回答

1

从iframe中跳出来,你可以做这样的事情:

// if the location of the top (topmost browser window) not equal to the current location then it redirects to the URL of the current window (iframe) 
if (window.top.location != document.location) { 
    window.top.location.href = document.location.href; 
} 
+0

“顶.location“,这个检测到底是什么? – factordog

+0

哦,等我明白了。所以逻辑是,它将检查它在url上的实际页面,然后如果它不等于我的客户端的指定站点,它将重定向? – factordog

+0

美丽。完美的作品 – factordog