Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript - Back button history

I have a Single Page Application running in PhoneGap, and driving through content with Sammy.js

Consider this:

The home page is domain.com/#/panel enter image description here

Then I click on Reglas(regulations) The location.hash updates to #/regulations

enter image description here

After this I can see the current regulations, when I click anyone of this, I update again the location.hash to #/regulation/1 (or whatever number the regulation is):

enter image description here

Here is the deal:

I have a back button inside my app in the navbar next to the screen title, and it have a click listener with this:

$('back').click(function(e) {
    e.preventDefault;
    // window.history.back();
    history.back();
});

It works well when I'm at the 2nd screen (#/regulations), it brings me back to #/panel. But when I'm on a 3rd screen (#/regulation/1) it brings again to #/panel instead of the #/regulations section.

When I click browser's backbutton it works very good but somehow when I click MY back button it doesn't work properly.

I have read some topics about this, like Mozilla's one: enter image description here

Like Mozilla said, it should work exactly as if I clicked the browser's back button, but it doesn't.

I have tried with:

window.history.back();
    window.history.go(-1);
    history.back();
    history.go(-1);

It seems not working with anything.

Can anyone help?

like image 373
mdelafuq Avatar asked Nov 10 '22 17:11

mdelafuq


1 Answers

Sorry little mistakes happens to anybody:

e.preventDefault(); (parentheses) and #back (sharp) were the problem.

However, if anybody get in this trouble, Mozilla had the reason, any of this lines work exactly as if the user clicks browser's back button:

$('#back').click(function(e) {
    e.preventDefault();
    window.history.back();
    // window.history.go(-1);
    // history.back();
    // history.go(-1);
});
like image 58
mdelafuq Avatar answered Nov 14 '22 21:11

mdelafuq