如何阻止javascript函数从框架内容

How to stop javascript function which is from iframed content?

本文关键字:框架 函数 何阻止 javascript      更新时间:2023-09-26

我正在构建另一个网站。但是他们的页面上有一个javascript函数,我不想在我的页面上执行。这是

if (top.location != location) {
top.location.href = document.location.href; }    

我试过了,但是整个页面都停止了。

function StopLoading() {
if (!document.all)
{
window.stop();
}
else
{
window.document.execCommand('Stop');
}
}    
<iframe id="i_frame" onload="StopLoading()" src="http://website.com/"></iframe>

我不相信你能做到这一点,由于标准的浏览器安全限制(假设涉及的2个站点在不同的域)。有了这种能力,就有可能发生相当恶劣的事情。你应该把这个网站放在一个框架里吗?

正如我在之前的回答中所说的,只有当您有一个能够返回HTTP/1.1 204 No Content标头的服务器时才能这样做。如果你有这样的服务器,那么你可以使用这样的代码:

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);

这个方法在这里描述。

如果没有204标头,您可能无法做到这一点。这是有原因的。帧破坏通常用于防止点击劫持,这是非常有害的。