【发布时间】:2011-11-27 12:28:34
【问题描述】:
我一直在研究 Frame busting buster(名字里有什么,呵呵),它让我的用户留在我的页面上并打开一个带有目标 URL 的新窗口。我正在使用 Lightbox 脚本来显示 iframe,这就是我正在做的事情:
1) 为所有 .lightbox 点击添加了一个事件,f.e:
$('.lightbox').live("click", function(e) {
e.preventDefault();
$('#redirectURL').val($(this).attr('href'));
$(this).lightbox();
}
2) 增加了一个框架破坏克星:
<script type="text/javascript">
var prevent_bust = 0
window.onbeforeunload = function() { prevent_bust++ }
setInterval(function() {
if (prevent_bust > 0) {
prevent_bust -= 2
window.top.location = 'http://server-which-responds-with-204.com'
}
}, 1)
</script>
3) 修改了框架破坏破坏代码以满足我的需要,它们是:
- 检测 iframe 是否要更改 window.top.location
- 如果是这样,请使用 204 服务器响应防止这种情况发生
- 打开一个新页面:
window.open( $('#redirectURL', '_blank' ); - 关闭灯箱:
$('.jquery-lightbox-button-close').click();
到目前为止,这是我想出的:
var prevent_bust = 0
window.onbeforeunload = function() { prevent_bust++ }
setInterval(function() {
if (prevent_bust > 0) {
prevent_bust -= 2;
redirectURL = $('#redirectURL').val();
if(redirectURL != "") {
window.top.location = 'http://www.****.com/ajax/nocontent.php';
window.open(redirectURL, "_blank");
$('.jquery-lightbox-button-close').click();
$('#redirectURL').val('');
} else {
window.top.location = 'http://www.****.com/ajax/nocontent.php';
}
}
}, 1);
// 编辑:Before I forget, 'nocontent.php' is a file that returns a 204 header
对于 Firefox,它就像我编写的那样,如果在 window.top.location 中检测到更改,它会打开一个新的框架/页面并阻止 iframe 重新加载顶部位置并将其四舍五入,它会关闭 jQuery灯箱。
Safari/Chrome 的行为类似,它们打开一个新的浏览器屏幕 (not sure if theres an option to say target="_newtab" or something?)。唯一不好的是他们并没有真正显示弹出窗口被阻止的消息,但我可以通过在我的网站上显示一个带有页面链接的弹出气球来解决这个问题。
Internet Explorer 真是令人震惊,只剩下一只害群之马。IE 不会打开新的弹出窗口,也不会阻止 iFrame 重置的 window.top.location,而是继续将整个页面刷新到 '#targetURL '。它与默认破坏代码相同。所以这不是因为我的一些编辑。
谁能发现我的代码中的错误?
另外,我需要进行一些修改,以查看请求是由 iframe 还是由用户本身发出的,因为现在确实没有用户可以通过更改工具栏中的地址来离开我的页面或通过单击链接,这并不是真正需要的 LOL。
提前致谢。
【问题讨论】:
-
我已经读过这个问题,是的,这就是我从那里得到框架破坏破坏代码的地方。还没有真正阅读任何答案,因为该用户想找到一个解决方案来击败框架破坏破坏者。
-
我会尽快研究一下,谢谢
-
实际上,即使是 Google 图片也无法通过 stackoverflow 的帧破坏来做到这一点,例如:www.google.fr/search?q=site%3Astackoverflow.com&tbm=isch
-
所以毕竟可能没有万无一失的解决方案..
标签: javascript jquery iframe framebusting