Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can JavaScript hook and override browser back and forward buttons?

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.

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?

Thanks

like image 480
Tyler Avatar asked Dec 21 '22 18:12

Tyler


1 Answers

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.

like image 121
meder omuraliev Avatar answered Jan 18 '23 23:01

meder omuraliev