How do you break the frame without breaking the browser button?

The site that links to mine saves my site in a frame, so I added the following JavaScript to my page:

if (window.top.location != window.location) { window.top.location = window.location } 

Now, if I get to my site through an intruder site, my site will successfully exit the frame. But the back button breaks! The "Back" button sends the user into the frame of the version of my site, which immediately breaks, returning him to where he was trying to go! Is there an easy way to fix this?

+6
source share
3 answers
 window.top.location.replace(window.location); 

The replacement method is specifically designed for this purpose. It replaces the current item in the history state with a new destination so that the back button does not go through the destination that you do not need.

+10
source
Answer

jfriend00 is really correct. Using the window.location.replace method will work without affecting the back button.

However, I would like to note that whenever you want to stop creating a page, you have to do more than just! There are several methods to prevent a simple script, such as leaving a frame that works in many modern browsers. Perhaps you can turn off the page, display a message with a link to the full page, something like this. You can also use the X-Frame-Options response header, which tells the browser not to display the page in frame. If you do not take some of these measures, you can click your site.

+2
source

Another solution is to open your site in a new window, leaving a friendly message on the iframed site:

 if (parent.frames.length) { window.open("mySite.htm", "MySite"); location.href= "framedMessage.htm"; } 

Where framedMessage.htm contains a friendly / warning message.

+1
source

Source: https://habr.com/ru/post/892536/


All Articles