Search code examples
javascriptformsbrowser-history

Tracking user browser history without server-side code


I'm developing a registration form that leads users to a "Thank you" confirmation page. On this confirmation page, I'd like to have a button that redirects users to wherever they were prior to the registration page.

The only catch is that my client needs this to be done entirely on the front-end, with HTML5 and JavaScript.

Given these constraints, what's the most efficient way to generate a link that sends the user to where he/she was two pages ago?


Solution

  • If I understand your question correctly, you can just do this when the button click:

    <script>
    function goBack()
      {
      window.history.go(-2)
      }
    </script>
    
    <body>
    <button onclick="goBack()">Go Back 2 Pages</button>
    </body>
    

    This was directly from example of w3school http://www.w3schools.com/jsref/met_his_go.asp