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 ?

  • Creating a custom “sync” method in Backbone.js
  • Read content of external script tag with jquery
  • Binding a Backbone Model to a Marionette ItemView - blocking .fetch()?
  • How can I hover on a rectangle in javascript?
  • Alternating between 2 different templates in backbone.js
  • What's the best way to override Model.get(attr) in Backbone.js?
  • Implementing a master view with multiple collections. Backbone.js
  • Google Map API BackBoneJS Cannot read property 'offsetWidth' of null
  • Backbone view can't use Backbone collection in typescript
  • The best way to sort a collection in a CompositeView
  • Easiest way to reset Backbone's model to initial defaults?
  • Backbone Router not triggering route
  • One Solution collect form web for “Backbone.js – Back button (window.history.back) with label of the previous page”

    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.