JavaScript可以钩住并覆盖浏览器后退和转发按钮吗? [英] Can JavaScript hook and override browser back and forward buttons?

查看:168
本文介绍了JavaScript可以钩住并覆盖浏览器后退和转发按钮吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在撰写一个网络应用程序作为我们后端的控制面板,我们完全使用JavaScript - 所有DOM元素都是动态创建的,没有静态HTML。

We're writing a web app to act as a control panel for our back end, we're solely using JavaScript - All DOM elements are created dynamically, no static HTML at all.

我们有一个模块/插件类型的接口,一个模块是视图的集合 - 一个视图本质上是一个包含一个初始化方法的函数,它接受一个div的arg,所以它知道在哪里绘制自己。

We have a module/plugin type interface, a module is a collection of "views" - A view is essentially a function that contains an initialize method which accepts an arg of a div so it knows where to draw itself.

随着用户浏览视图,我们保留了他们曾经和他们在哪里等等的历史记录,所以我们可以提供视图之间的后向/后向导航作为导航时保存视图的状态。

As a user browses between views, we maintain a history of where they've been and where they are etc. so we can provide back/forward navigation between views as well as saving the state of the views as they navigate.

我们提供了一个面包屑轨迹以及后退和前进按钮。我还要做的是将浏览器的后退/前进按钮挂钩,这样我们可以拦截点击并覆盖它,以便浏览我们维护的历史记录 - 当他们进入我们维护的历史记录的第一页时我们当然不会覆盖,让他们离开我们的网站。

We provide a breadcrumb trail as well as back and forward buttons. What I'd also like to do is hook the browser back/forward buttons so we can intercept clicks and override it so it browses through the history we're maintaining - When they get to the very first page of the history we're maintaining we would of course not override and let them navigate away from our site.

有一种常见的方法吗?或者浏览器明确地阻止这种事情?

Is there a common way to do this? Or do browsers explicitly prevent this sort of thing?

谢谢

推荐答案

使用Ben Alman的很棒的jQuery-bbq插件: http://benalman.com/projects/jquery- bbq-plugin / ,它提供了一个与后退/转发按钮结合使用的hashchange跨浏览器。

Use Ben Alman's great jQuery-bbq plugin: http://benalman.com/projects/jquery-bbq-plugin/ which provides a hashchange cross-browser that is used in conjunction with back/forward buttons.

这篇关于JavaScript可以钩住并覆盖浏览器后退和转发按钮吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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