从Iframe内部加载网站时重定向页面?

时间:2016-04-15 08:36:59

标签: javascript redirect

我的情况是我创建了一个网站,客户端不希望其他开发人员能够使用iframe,但是当有人使用iframe时,它会将用户重定向到iframe试图通过的网站。

我一直在查看代码,这些代码可以让我检测客户端网站是否在iframe中加载,然后将其重定向回该网站。作为JS场景的新手,我并不是100%肯定如何解决这个问题。

到目前为止,我已将重定向代码放入document.ready function

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

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

提前致谢!

1 个答案:

答案 0 :(得分: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;
}
相关问题