Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in History.js to know when the back button was pressed

I've started to test History.js. After understanding how it works and that there is no popstate, instead there is statechange. I'm looking for a way to differ when the back button of the browser has been pressed.

The reason is that I need to know the URL before the state moved, from the one I'm going to. With the gist the project includes, only the URL we go to is looked.

I hope the solution is not to track latest URL visited in a global variable.

Thanks

like image 832
maraujop Avatar asked Nov 21 '11 11:11

maraujop


People also ask

What happens when browser Back button is pressed?

For pages that are set as non-cached, the browser reloads the page from the server when you press Back, as though it was the first time you are visiting it. For cached pages, the browser displays it out of the cache, which is much faster.

What is history back ()?

The History. back() method causes the browser to move back one page in the session history. It has the same effect as calling history.go(-1) . If there is no previous page, this method call does nothing.

Can I use Window history back?

Window History Backback() method loads the previous URL in the history list. This is the same as clicking the Back button in the browser.


2 Answers

I found the solutions on github to be a bit overdone for my purposes. I created a bool that is always true except right before when I used History to change the state.

var manualStateChange = true;

History.Adapter.bind(window,'statechange',function(){
    if(manualStateChange == true){
     // BACK BUTTON WAS PRESSED
    }
    manualStateChange = true;
});

Any time I change the state programmatically, set the bool to false:

manualStateChange = false;
History.pushState(null, null, currentPath);
like image 143
ashack Avatar answered Oct 03 '22 06:10

ashack


I know this is a pretty dated question, but I came up to a solution to issues with the back/forward button when switching between a standard page and a History-state page.

Scenario: Use HTML5 History (or history.js plugin) on a set of pages Then other pages we need real loads, aka a standard page (don't ask why, but there are certain use cases that this may be needed)

When you go from a History-state page, and do a real load to a standard page. You cannot go BACK: it changes the url, but doesn't load the page--it only fires a statechange; which I assume is the issue of the original post. My personal opinion is that this is a browser bug (all browsers have the issue, though), because the browser should know the page you're on was reloaded outside of the History-state page.

I fixed this with something really simple: Listening to the statechange event and tell the browser to refresh when it does fire. This way I don't care if they go back or forward out of this page. If the browser thinks the state is changing (links don't fire the statechange event), only back/forward INTO a History state page fires this event, so it solves the issue.

Code, using jQuery + History.js plugin:

$(window).on('statechange', function() {
    window.location.reload();
});

If this doesn't make sense, you're probably not having this issue. However, I've noticed this on many sites that do use HTML 5 History (even pinterest.com has this issue if you reload when on an image modal and then try to go back).

Hopefully, if you do have this issue, you'll find this answer and have a huge sigh of relief :)

like image 26
James Wagoner Avatar answered Oct 03 '22 06:10

James Wagoner