从外部 iFrame 重定向和链接中删除历史记录

Remove history from External iFrame redirects and links

我有一个指向外部 link 的 iframe,它会在 iFrame 加载后立即进行一些重定向。这会导致在浏览器中添加历史记录,并且当用户单击 iframe 内的 links 时也是如此。

这会导致奇怪的用户体验,因为您有额外的历史记录状态指向同一个父页面。

我想知道是否有办法阻止 iframe 向浏览器添加历史记录?

您可以尝试使用 sandbox="" 属性来防止 iframe 重定向。

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

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

由于 link 是外部的,简短的回答是否定的。如果您可以控制 iframe 的内容,您可以做一些事情。将 iframe 添加到 DOM 后,只要 iframe 的 src 属性发生更改,或者如果用户在 iframe 中关注 link,历史记录就会更新。通过控制 iframe 内容,当用户单击 link 时,您可以使用 postMessage 并让父 [​​=17=] 将 iframe 元素替换为新元素。在同一个域中,您可以只使用 window.parent.

http://jsbin.com/coregedoze/2 这是我的代码的 link,带有 iframe,它没有 sandbox 属性 - 因为此属性仅适用于 HTML5 和有限的浏览器版本。我重新检查了这段代码,它不会保存任何历史记录。唯一的情况是,如果您更改 iframe 的源,整个页面将重新加载并进入历史记录,这可以使用 sandbox="value" 避免,如 JAYDEN LAWSON 的上述回答所述。另一种方法是在 iframe 之外的页面中声明您的 links 并在 iframe 中调用它们,如 traget="iframe_a" ,其中 iframe_a 是您的 iframe 名称。希望这有帮助:-)

不,由于 XXS

,对于将 src 设置为另一个域的 iframe,您几乎无能为力