防止在 iframe 中导航更改父站点历史记录 [英] Prevent navigation in iframe change parent sites history

查看:53
本文介绍了防止在 iframe 中导航更改父站点历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,其中有一个包含一些管理功能的 iframe.

I have a web page where there is an iframe that contains some admin functions.

现在,当我在 iframe 中导航(GET 和 POST 请求)时,这些操作会在浏览器历史记录中创建条目.所以现在当我想进入上一页的父页面时,它不会发生,因为浏览器 (Chrome) 开始返回 iframe 历史记录.

Now when I navigate (GET and POST requests) in the iframe these actions create entries in browsers history. So now when I want to get to the parent pages previous page it wont happen, because browser (Chrome) starts going back in the iframes history.

那么有没有办法克服这种行为?我希望浏览器导航在主页上工作 - 而不是在 iframe 中.

So is there a way to overcome this behaviour? I want browsers navigation to work on main page - not in iframe.

推荐答案

我认为没有办法阻止浏览器维护历史记录,但对于同源内容,您可以操纵历史记录的内容.

I think there's no way to prevent browser from maintaining the history but for same origin content you can manipulate the contents of the history.

基本上你可以监控事件 popstate 并使用 history.replaceState() 推送您自己的历史记录,该历史记录应自动替换浏览器记录的真实历史记录.请注意,您无法在没有导航的情况下从历史记录中删除项目,但您只能将一项替换为另一项.

Basically you can monitor event popstate and use history.replaceState() to push your own history that should replace the real history recorded by the browser automatically. Note that you cannot remove items from the history without navigation but you can only replace one item with another.

有关示例,请参阅:处理 iframe 内的历史导航

如果您不想操纵历史记录并且需要阻止浏览器维护所有历史记录,则应仅使用 XHR/ajax 而不是普通的链接和表单.

If you don't want to manipulate the history and need to prevent all history maintenance by the browser, you should use only XHR/ajax instead of normal links and forms.

这篇关于防止在 iframe 中导航更改父站点历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆