Close active panel on navigating backwards
The code below is used to
close active panel when navigating backwards in history (browser's back button, data-rel="back", $.mobile.back(), etc.) and cancels navigation. The code works as it should except for one issue that it clears "forward" history.
- var newUrl;
- $(window).on("hashchange", function (e) {
- newUrl = e.originalEvent.newURL;
- }).on("popstate", function (e) {
- var direction = e.historyState.direction == "back" ? true : false,
- activePanel = $(".ui-panel-open").length > 0 ? true : false,
- url = newUrl,
- title = document.title;
- if (direction && activePanel) {
- $(".ui-panel-open").panel("close");
- $(".ui-header .ui-btn-active").removeClass("ui-btn-active");
- $.mobile.pageContainer.pagecontainer("change", url, {
- allowSamePageTransition: true
- });
- window.history.replaceState({}, title, url);
- return false;
- }
- });
-
- $(document).on("pagebeforechange", function (e, data) {
- if (data.options && data.options.allowSamePageTransition) {
- data.options.transition = "none";
- } else {
- data.options.transition = $.mobile.defaultPageTransition;
- }
- });
To reproduce the problem:
- Navigate to "Page 2"
- Navigate to "Page 3"
- Navigate back to "Page 2"
- Notice browser's "forward" button
- On "Page2", open "panel" and then hit browser's back button or "Back" button in header
You will notice that browser's "forward" button is disabled.
How to maintain "forward" in browser's history?
Update:
Here is another version where I update
history.state through
.replaceState(). It has no effect whatsoever except for maintaining browser's history.