javascriptbackbone.jsbackbone-events

Backbone.js - Back button (window.history.back) with label of the previous page


I'd like to display a back button with a label related to the previous page (a title or something like this) which will be called on the click event. I currently use the window.history.back() function on the event but how could I get the label ?


Solution

  • JS out-of-the-box allows to ask for the previous URL but not for the previous title.

    You can use other custom implementations. For example with cookies:

    // code simplified and no tested
    function pushHistory() {
      var history = getHistory();
      var newHistory = {
        url:   window.location.pathname,
        title: $("title").text()
      }
      history.push( newHistory );
    
      $.cookie( "app.history", JSON.parse( history ) );
    }
    
    function getHistory(){
      return JSON.parse( $.cookie( "app.history" ) );
    }
    

    Note: I'm using jquery-cookie plugin to simplify the code.

    Now is up to you to call pushHistory() in every page your user visits, and take the proper element in your "app.history" cookie to show the info.