snipt

Ctrl+h for KB shortcuts

Text only

JavaScript iFrame Buster

1
2
3
4
5
6
7
/*
Oftentimes you want to make sure your site isn't being IFRAME'd.  Sometimes it's for security reasons, other times it's so your site's content isn't being skimmed else, and other times it's...oh, who cares, you just want to do it.  Here's a short way to escape frames:
*/

if (top.location != self.location) {
    top.location = self.location.href;
}
https://snipt.net/embed/ec92d1593c098837461fbc10ed5230fe/
/raw/ec92d1593c098837461fbc10ed5230fe/
ec92d1593c098837461fbc10ed5230fe
text
Text only
7
2019-07-19T04:13:11
True
False
False
Feb 18, 2014 at 10:11 AM
/api/public/snipt/114762/
javascript-iframe-buster
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>/* </span><span id="L-2"><a name="L-2"></a>Oftentimes you want to make sure your site isn&#39;t being IFRAME&#39;d. Sometimes it&#39;s for security reasons, other times it&#39;s so your site&#39;s content isn&#39;t being skimmed else, and other times it&#39;s...oh, who cares, you just want to do it. Here&#39;s a short way to escape frames: </span><span id="L-3"><a name="L-3"></a>*/ </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a>if (top.location != self.location) { </span><span id="L-6"><a name="L-6"></a> top.location = self.location.href; </span><span id="L-7"><a name="L-7"></a>} </span></pre></div> </td></tr></table>
js