15

I am trying to implement History.js for my ajax site so that I can use forward and back buttons and even bookmarks. However the example @ https://github.com/browserstate/History.js/ has me a bit confused as into how to implement it. Does anyone have a simple tutorial or example on how to use this. An example we can use to start the example is a navigation link such as

<script type="text/javascript">
window.onload = function() 
{
function1();
};

<ul>
<li><a href="javascript:function1()">Function1</a></li>
<li><a href="javascript:function2('param', 'param')"</a></li>
</ul>
balupton
  • 47,113
  • 32
  • 131
  • 182
Kern Elliott
  • 1,659
  • 5
  • 41
  • 65
  • See a possible answer and related question here: http://stackoverflow.com/questions/13278822/is-this-a-proper-way-to-use-history-js – dansalmo Nov 07 '12 at 21:41

2 Answers2

36

I had trouble fully understanding how to use History.js. Here is some code from their wiki with my comments for explanation:

1. Get a reference to the history.js object

To get a reference to the History.js object reference window.History (Capitol 'H').

var History = window.History;

To check if HTML5 history is enabled check History.enabled. History.js will still work using hash tags if it is not.

History.enabled

2. Listen for history changes and update your view from here

To listen for history state changes bind to the statechange event of the Adapter object.

History.Adapter.bind(window,'statechange',function(){ 
    var State = History.getState(); 
    History.log(State.data, State.title, State.url);
});

3. Manipulate history states by using push or replace

To add a state to the history, call pushState. This will add a state to the end of the history stack which will trigger the 'statechange' event as shown above.

History.pushState({data:"any kind of data object"}, "State Title", "?urlforthestate=1"); 

To replace a state to the history, call replaceState. This will replace the last state in the history stack which will trigger the 'statechange' event as shown above.

History.replaceState({data:"any kind of data object"}, "State Title", "?urlforthestate=1"); 

The difference between pushState and replaceState is that pushState will add a state to the history list, replaceState will overwrite the last state.

NOTE: pushState and replaceState serialize the data object, so use minimal data there.

4. If you want to add additional ui for the user to be able to navigate the history, use the navigation commands

Use back and go to navigate the history via code.

History.back();
History.go(2);

Additional: Using "a" tags with history

To use "a" tags with history you will need to intercept click events and prevent the browser from navigating by using the event.preventDefault() method.

Example:

<a href="dogs/cats" title="Dogs and cats">Dogs and Cats</a>

$('a')​.click(function(e){
    e.preventDefault();
    var url = $(this).attr('href');
    var title = $(this).attr('title');
    History.pushState({data:title}, title, url);
})​;

I hope this helps.

zachzurn
  • 2,161
  • 14
  • 26
  • I am also struggling with this as an example. I get what is shown above, but there is a lot that is missing compared to the previous history.js that used the # method. I was able to get previous # based examples working by binding to clickable items such as an anchor in a nav menu. This would then call a handler based on the href where the ajax call could be made. I do not see how the click events for each url get bound to here. Everything I have tried does not intercept the url click events. – dansalmo Nov 07 '12 at 19:12
  • What you need to do is intercept the clicks on those hrefs and go from there. Example: ​$('a')​.click(function(e){ e.preventDefault(); })​. After the e.preventDefault() you can take the value of the href attribute and use it to push history states without navigating away from the page. – zachzurn Nov 07 '12 at 19:43
  • Thanks, I did something like that and added it above but I am not sure if it is really the proper way. Also, I don't see why e.preventDefault() is needed. It seems that return false; does the same thing. – dansalmo Nov 07 '12 at 21:44
  • The difference between using the hash tags and not using them is that the browser will not naturally refresh when the href is a hash. If you are using only the hash style then you can just let the browser take care of changing the url for you. – zachzurn Nov 07 '12 at 23:30
  • How will I load the contents of the URL in the pushState – user962206 Dec 25 '12 at 04:48
  • Can you add an addendum on `popstate` just to have a comprehensive review? – J82 Jan 19 '15 at 15:55
3

Refer this question: Implementing "Back button" on jquery ajax calls

Some more info related to RSH

How to implement RSH (Really Simple History) for Ajax History Back Button

Community
  • 1
  • 1
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • ok the tutorial helped alot just one question concerning it in jquery is $('#').load() the equivalent of an ajax request such as onlick='javascript:function1();' ? – Kern Elliott Oct 15 '11 at 14:48