Prevent your website being displayed inside IFRAME

Posted on June 23, 2008 
Filed Under how-to, javascript, tips and technique

Sites like Google Image search and goodphptutorials.com displays the website under the IFRAME and I don’t think you guyz want your site to be displayed it under the IFRAME. If you want to protect your site to be displayed under the IFRAME then here is the tips for you.

 

JavaScript Code Prevent web page being displayed inside iframe

Place the following piece of code in JavaScript to those pages which you want to prevent being displayed inside the IFRAME.

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

The above code is simple and straightforward. First of all, it is checked that if url of the top frame is different to the URL containing the JavaScript code, if they are different then the website comes out of the IFRAME and gets displayed in the browser. This is how we can prevent your web page being displayed inside IFRAME but note that javascript must be enabled otherwise the code won’t take any action.

Popularity: 6% [?]

Enter your email address and get recent tutorials, tips, tricks and scripts of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» Prevent Directory Listing using .htaccess
» Hide actual url in address-bar using iframe
» Prevent form post request from another domain in PHP
» Change default directory page using .htaccess

Comments

4 Responses to “Prevent your website being displayed inside IFRAME”

  1. Arwind Prasad on June 24th, 2008 1:54 pm

    I was looking for same solution.
    Thanks Alot……

  2. subesh on June 25th, 2008 11:50 am

    What If javascript is off…?. Can’t we do it from the doctype of the document……defining it strict…rather than transitional or loose

  3. Roshan on June 25th, 2008 4:33 pm

    subesh….if javascript is turned of then noone can help you in that sitaution..

    And Doctype can’t handle such scenario, doctype is a standard for how presentation is going to be handled by browser (basically css) and is a standard of using valid tags according to DTD specifications in the document…

  4. arty on July 2nd, 2008 7:22 pm

    This way is oldschool but it still works … the good old frame braker.
    By the way this is not only for iframes … i would say it works for frames in general :)

Leave a Reply