Note that there are some explanatory texts on larger screens.

plurals
  1. POFrame breaking only cross-domain but not for iframes from the same origin?
    primarykey
    data
    text
    <p>This question was previously <a href="https://stackoverflow.com/questions/952242/why-do-frame-breakers-work-cross-domain-and-can-you-conditionally-use-frame-brea">asked and answered</a> correctly, but there did not seem to be a solution posted.</p> <p>If a site has iframes, and one wants to prevent those from being enclosed in a frame from a different domain, simplistic frame-busting will not be useful:</p> <pre><code>&lt;script&gt;if (top != self) top.location = location&lt;/script&gt; </code></pre> <p>However, since cross-frame scripting to other domains should generate exceptions, something like this seems to work well inside the iframe:</p> <pre><code>&lt;script&gt; try { if (window.document.domain != top.document.domain) { // throws exception throw "You naughty puppy!"; // Should not ever get here, right? } } catch () { top.location = "/error/naughtypuppy"; } &lt;/script&gt; </code></pre> <p>The <code>if</code> above should be enough on its own to prevent cross-domain framing of iframes. It should only ever return <code>false</code> or throw an exception, so is there anyway the script could reach the <code>throw</code> statement in a browser?</p> <p>Would this be sufficient to prevent framing only from other domains?</p> <pre><code>&lt;script&gt; try { var bogus = top.document.domain; } catch () { top.location = "/error/naughtypuppy"; } &lt;/script&gt; </code></pre> <p>Edit: A similar solution is hinted at here, but one would not rely on the parent frame to include the frame-busting code. <a href="https://stackoverflow.com/questions/2365822/detect-when-iframe-is-cross-domain-then-bust-out-of-it">Detect when iframe is cross-domain, then bust out of it</a> . Essentially the same solution as "try to access the other frame and bust if an exception occurs."</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.
 

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