admin管理员组

文章数量:1287594

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=""}};

Is this making sure the page isn't rendering inside of a iframe?

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=""}};

Is this making sure the page isn't rendering inside of a iframe?

Share Improve this question edited Feb 1, 2011 at 14:57 Sachin Shanbhag 55.5k11 gold badges91 silver badges103 bronze badges asked Feb 1, 2011 at 14:53 BlankmanBlankman 267k330 gold badges795 silver badges1.2k bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

Yes it would seem that this script attempts makes itself the parent frame if it isn't already. AS Pointy has said, these don't always work. Javascript cannot "break out" of it's owner document in most browsers unless it created that document. Or if the page containing the Javascript was loaded dynamically in such a way that the Same Domain policy isn't in effect. (Likely not the case if your site is in an iframe/frame) In the case of a page that is being opened inside another, the script should not have access to modify the parent document.

Here's the code you posted broken out onto several lines with ments.

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.
};

It looks like a "frame buster" routine. Whether such things work seems to be a controversial topic.

window.top stands for the topmost window in your browser. In Simple words, its the browser window itself.

The code seems to be clearing all the iframes with blank.

Is this making sure the page isn't rendering inside of a iframe?

An iframe or a frame, yeah. It's trying to break out.

本文标签: javascriptwhat is this windowtop check performing is this checking for an iframeStack Overflow