Last updated on February 2nd, 2022 at 08:12 pm

Using javascript protect your website from iframe of other website. 3rd party website cannot embed your website in iframe if you use this code. If you want to make sure your web pages are not included in other website frames, use this break_out_frame code to stop the madness.

I would prefer to place the javascript function into a separate file with a name like ‘functions.js’. This is how the code would look inside the ‘functions.js’ file:

function jump_out_frames()
if (top.location != location) {
alert("I am inside a Iframe, You cannot do this")
	top.location.href = document.location.href ;

Now let us include this javascript function file into your web page, place the following code in the within the <head></head> section. Let us assume that the web page name is out_frame.html

<script src="functions_new.js" language="JavaScript" type="text/javascript">

To trigger the breakout of frame javascript function, you need to call that function from within the body tag of the web page out_frame.html

<body onload="jump_out_frames()">

You are all set, Assuming that your website / web page is this , Now let’s think of a scenario in which another website try to load your web page out_frame.html inside their website iframe. Click Here to see the above script in action. The webpage will basically exit from the iframe and load itself

Leave a Reply

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