Skip to content

How to break out of an iframe

So someone’s trying to make their site better by opening a page on your site inside of their site using an iframe?

They’re doing something like this:

<iframe src="" width="100%" height="100%"></iframe>
Code language: HTML, XML (xml)

Well how about you stop that from happening by pasting the following line in your website’s header!

<script type="text/javascript"> if (top.location != self.location) { top.location = self.location.href; } </script>
Code language: HTML, XML (xml)

The above code basically does a check to see if the site is the same as what is said in the address bar, if not then it sets the parent’s frame (the other site) to change to your site’s location!

Make sure to paste that just before the closing </head> tag near the top of your source code.

See also  How to tell if a String contains a Substring in Javascript
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x