Prevent your site from being shown in iFrames using javascript

Data scraping is one of many pains bloggers and web developers face these days. Someone works hard enough to create a unique content and a unique code or script making their website useful and someone else simply uses iframes to show all their effort on their own website and getting all the credit.

Or for any other reasons, we might want to prevent our webpages to be shown in iframes too, phishing attacks for example.

There are many fake sites which pretend to be the original site showing the reviews or previous shown on someone elses site by showing their site on their page using iframe.

They steal your bandwidth and your effort to gain traffic for themselves and getting benefits, all at your cost.

This can be specially annoying and frustrating when the site who is scraping your site outranks your original site. Despite googles effort to report scrappers, they still persist and the spammers continue to utilize techniques to show your work as theirs.

So back to the point, how to stop them from doing so ? Well the way we will be discussing here is using a bunch of javascript code which will atlas “try” to check if your page is being shown within an frame and then it will replace that websites url with your own site url, inshort, taking the user from their site directly to your own site, thus bringing the traffic to your original site from that scrapper site. The methods employed for this purpose are named “frame killer / iframe killer” or “frame killer / iframe buster”.
You need to place the following code in head of your site (header.php of your theme in wordpress) just above the <body> tag.

Note: This code works for all current browsers.

<script>
if (self == top) {
var theBody = document.getElementsByTagName(‘body’)[0]
theBody.style.display = “block”
} else {
top.location = self.location
}
</script>

This method should work for blocking out and fixing most of the scrapers out there who try to steal your effort, traffic and bandwidth.

However if the above code don’t work in your specific case, you can also try the following code and test if it helps:

<script type=”text/javascript”>

var self = self.location;
var top = top.location;
var parent = parent.location;
var window = window.location;

if (top != self || parent != self || window != self )
window = self;
</script>

Which solution do you use to stop scrappers from scrapping and showing your website or blog in an iframe ?

Leave a Reply

Your email address will not be published.