Remove history from External iFrame redirects and

2019-02-27 03:05发布

问题:

I have an iframe pointing to an external link that does some redirections as soon as the iFrame loads. This causes added history into the browser and also when users click on links inside the iframe.

This causes a weird user experience because you have extra back history states pointing to the same parent page.

I was wondering if there was a way to prevent the iframe from ever adding history to the browser?

回答1:

Since the link is external, the short answer is no. If you are in control of the content of the iframe there are a few things you can do. History will be updated any time the src attribute of an iframe is changed after it has been added to the DOM or if the user follows a link in the iframe. With control of the iframe content, when a user clicked a link, you could use postMessage and have the parent window replace the iframe element with a new one. In the same domain, you could just use window.parent.



回答2:

You can try to use sandbox="" attribute to prevent the iframe from redirecting.

<iframe src="http://example.com/" sandbox=""></iframe>


回答3:

There is no way to clear the session history or to disable the back/forward navigation from unprivileged code. The closest available solution is the location.replace() method, which replaces the current item of the session history with the provided URL.

Source (MDN)

location.replace() | MDN



回答4:

http://jsbin.com/coregedoze/2 Here is a link of my code with an iframe, this has no sandbox attribute- as this attribute only works with HTML5 and limited browser version. I rechecked this code and it wont save any history. Only case is if you change your source of the iframe the whole page reloads and makes another entry into history, which can be avoided using sandbox="value" as mentioned in above answer by JAYDEN LAWSON. Another way around is declare your links in your page out of iframe and call them in iframe like traget="iframe_a" , where iframe_a is your iframe name. hope this helps :-)



回答5:

No, You can do almost nothing with an iframe which has src set to another domain because of XXS