How to disable your website accessing from iframe using JavaScript?

We use iframe tag to load any other link or website on our web page. In the same way, any other website can load your website or a page from your website on their web page using the HTML iframe tag.

Now suppose you don’t want your website to be loaded within an iframe tag by anyone. You want to disable your website accessing from the iframe┬átag. So how to do that?

Live word counter from HTML textarea in JavaScript

Locate the Geo-position of a visitor in JavaScript

Here in this post, we are going to see how to disable a web page from being loaded via iframe. To disable a website from accessing via iframe we are going to use JavaScript code.



JavaScript code that will disable your website accessing in iframe

Below is the JavaScript code that will disable your website accessing in the iframe:

<script>
 if(self != top) { 
  document.querySelector("html").innerHTML = "";
  top.location = self.location;
 }
</script>

With the above code if your website accessed via iframe then the JavaScript code will redirect to the URL of your web page. The code will first check if your site is accessed in iframe or not and if it found your web page is accessing in the iframe tag, then it will redirect to the URL. We have used JavaScript self-property in this code.

Close current tab in browser window using JavaScript

Stop form submission using JavaScript preventDefault() Event Method

Leave a Reply

Your email address will not be published. Required fields are marked *