这个 window.top 检查正在执行什么?这是检查 iframe 吗?

Bla*_*man 4 javascript jquery

if(window.top!==window.self){document.write="";
window.top.location=window.self.location;
setTimeout(function(){document.body.innerHTML=""},1);
window.self.onload=function(){document.body.innerHTML=""}};
Run Code Online (Sandbox Code Playgroud)

这是确保页面不会在 iframe 内呈现吗?

sho*_*ger 5

是的,如果这个脚本还没有成为父框架,那么它似乎会尝试将自己设为父框架。正如 Pointy 所说,这些并不总是有效。在大多数浏览器中,Javascript 无法“突破”其所有者文档,除非它创建了该文档。或者,如果包含 Javascript 的页面是以相同域策略无效的方式动态加载的。(如果您的网站位于 iframe/frame 中,情况可能并非如此)如果页面在另一个页面中打开,则脚本不应有权修改父文档。

这是您发布的代码,分为几行并带有注释。

if(window.top!==window.self){ // is the outermost document not this document?
  document.write=""; // write an empty text node to the document. (not sure why)
  window.top.location=window.self.location; // set the parent document's location to this document's location.
  setTimeout( function(){document.body.innerHTML=""}, 1); // after 1 millisecond make this document's HTML empty. (Probably a memory leak fix)
  window.self.onload = function(){document.body.innerHTML=""} // Another memory leak fix to clear the current document when it is done loading.
};
Run Code Online (Sandbox Code Playgroud)