Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between back button pressed and forward button pressed with History API

I'm using the history API to push a new URL to the web page without reloading it. I have multiple buttons that all have different functionality.

My script now works almost without problems. When I press the button something happens, and when I go back the script fires event listener without reloading the page.

However, when I now press the forward button, I want to go forward. The URL is changed correctly to the next one, but the event listener still fires as if the back button was pressed

Example:

  1. index1.html
  2. button press → index2.html
  3. button press → index3.html
  4. back button pressed → index2.html
  5. forward button pressed → URL is now index3.html, but the content is index1.html

I guess this is because I have a listener, that listens for popstate which happens for back button and forward button pressed. How can I differ what kind of button was pressed?

This is the part that binds the listener:

if (window.history && window.history.pushState) {
    $(window).unbind('popstate');
    $(window).bind('popstate', function (e) {
        clearOverlays();
        var url = URL
        $.ajax ( {
            url : url
        }).done ( function ( data ) {
            console.log(data);
        });
    });
}
like image 762
Musterknabe Avatar asked May 19 '14 07:05

Musterknabe


People also ask

What is the difference between back button and forward button?

The forward button is used to go to the next page while the back button is used to go to the previous page.

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 the forward button?

When referring to e-mail, forward is a button or feature that lets you send a received e-mail to another e-mail address.

What is forward button in browser?

If you've visited previous or even earlier pages within the browsing sessions, then you can move forward and go to the last page until the button is grayed out. The forward button serves a great purpose for easy navigation on Chrome.


1 Answers

Here is a way to simplify your code:

You can use the built-in <a> tag with a nested <input> tag in order to navigate between pages. Window.open() will create a popup, and window.location.replace() will disable the history API, neither of which would help you, so a nested <input> tag (inside of the <a> tag) is the most logical solution.

As for distinguishing the buttons, you could use the HTML onclick attribute in each button to specify the JS function to execute, in your case window.history.back() and window.history.forward().

For instance, you could use the following.

<!DOCTYPE html>
<!-- Example of code for your first page -->
<html lang="en">

<head>
  <!-- Metadata -->
</head>

<body>
  <h1>Page 1.html</h1>
  <!-- The <a> tag below is what transitions between pages. The <input> tag is just for the appearance of a button. -->
  <a href="yourlink.html"><input type="button" value="page2"></a>
  <!-- Input tags below are required, unless you want to use 'javascript:(function)' in an <a> tag -->
  <input type="button" onclick="window.history.back()" value="Go back">
  <input type="button" onclick="window.history.forward()" value="Go forward">
</body>

</html>
like image 69
Ben Z. Avatar answered Sep 19 '22 06:09

Ben Z.