Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If all you're trying to do is detect when your code is in a frame of any kind, all you need is this:</p> <pre><code>if (window.top !== window) { // you are embedded in a frame } </code></pre> <p>or to set your variable <code>inFrame</code> like this:</p> <pre><code>inFrame = (window.top !== window); </code></pre> <p>or in function form:</p> <pre><code>function isInFrame() { return (window.top !== window); } </code></pre> <p>As long as you aren't accessing properties on the <code>window.top</code> object, you shouldn't need exception handlers to protect from cross domain parents. In a cross-origin situation, you can access <code>window.top</code>, but you cannot access properties such as <code>window.top.location.href</code>.</p> <p>As for your other questions:</p> <blockquote> <p>Is there a possibility that this code to fail [report true on false or vice versa]?</p> </blockquote> <p>The first part of your code looks OK though the exception handler is not required. The second part of your code will throw an exception every time in modern browsers when the window and parent frame are different origins.</p> <blockquote> <p>Will there be any difference while window or document is being loaded in the properties [self, location, top, parent] I use to check? Because my script loads and executes synchronously mostly before the document is ready or window is completely loaded and that does the iniframe test to proceed further.</p> </blockquote> <p>At the point your window is loading, it is safe to make this check. Both <code>window</code> and <code>window.top</code> will be valid at that point even if neither has completed loading yet. So, there should not be any issues with inFrame detection that require you to wait for anything to complete loading.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload