Automatically break out of an iframe

Iframe stands for inline frame. An Iframe is a floating frame that can be inserted anywhere within a web page.

A concern for webmasters about iframes is that an iframe can be used to include pages on your website into external sites.

How do you prevent pages on your website from being included through an iframe by another website?

Placing the following javascript code at the top of all pages on your site will ensure that if any other site iframes a page on your website, that your page will break out of the iframe and just display your page in the users browser.

<script type="text/javascript">
	<!--
		if (top.location!= self.location) {
			top.location = self.location.href
		}
	//-->
</script>

The best way to implement this break out of an iframe code on an entire site is to put the code into an external javascript file, and include it in a common template file (i.e. header template file that is included by all pages on your website).

A working example of how to break out of an iframe. You will be redirected to http://usabledesigns.com/demo/iframe-content if you have javascript enabled in your browser. You can view the original page by turning off javascript in your browser.

The only downfall with this approach to breaking out of Iframe's is when a user has javascript disabled / unsupported in their browser.

Tested in Firefox, IE5 - IE7, and Konqueror.

For assistance with adding this to your website or for any other website development services, please contact UsableLayout.

Comments

very very thankful

I'm very tense about this problem,but after visiting this site i'm so happy.
thanking you

Fantastic thank you.

Fantastic thank you.

Thnaks!

Keep up good work, this site is wonderful!With the best regards!

I had an interesting problem

I had an interesting problem wherein I had to write a page that used an iframe to pull in a form page from another server (where I could use aspmail) and then, on successful posting, break the visitor out of the iframe and back to the home page.
All I did was write 'target="top"' into the resolve of the form page and it worked first time.

Work great:)

Thanks.. i't work in my frame. I use http://www.c-o.in/ and i can break his frame to my personal page..

Thanks this worked great for

Thanks this worked great for me! - Eric

Thanks for a very helpful

Thanks for a very helpful piece of information!

Additionally I have a question to you. Is there a less "brutal" possibility to break out an iframe? Imagine there is an iframe B inside an iframe A. Is there a probability to break out of the iframe B, but still to stay in iframe A?

Thank you in advance!

Best regards,
Daniel

No ... not possible. This

No ... not possible. This coding will simply keep doing its magic until completely free. Going from iframe B to iframe A then out.

The one exception to this is when Javascript has been disabled, which, sadly, more and more firewall and AV programs are permitting.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options