I am getting familiar with the html5 History API, but I am using history.js to extend the compatibility,
I have one question and it's that, how can I know in:
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
var State = History.getState(); // Note: We are using History.getState() instead of event.state
/* Condition here to know if this change is a back or next button, and wich one?? */
});
This is my "whole" code...
var the = this;
var History = window.History;
if ( !History.enabled ) {
return false;
}
/* Store the initial content*/
History.replaceState({
content: $('#main').html()
}, document.title, document.location.href);
/* Bind to StateChange Event */
History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
var State = History.getState(); // Note: We are using History.getState() instead of event.state
//console.log(State);
//History.log(State.data, State.title, State.url);
console.log(history.length);
});
/* triggers */
$(document).on('click','a',function(e){
e.preventDefault();
var href = $(this).attr('href');
var title = $(this).text();
if(href == '#')
href = title;
$.get('portfolio.html',function(data, response){
var html = $(data).find('#main-content').html();
//console.log(html);
$('#ajax-load').html(html);
History.pushState({ content: html}, title, href);
$('#ajax-load').css({ position:'absolute',
right: -$(window).width(),
top: the.header.height(),
width: $(window).width(),
zIndex:999
}).animate({ right: 0 },300,function(){
console.log($('#main-content').length);
console.log($('#ajax-load').length);
$('#main-content').html(html);
$('#ajax-load').html('');
});
});
});
Because the only reason I should actually check for the history is for the NEXT/BACK button, right? otherwise the anchor href rules
-EDIT-
basically i need the condition from here
History.Adapter.bind(window,'statechange',function(){
var State = History.getState();
var condition = false;
if(condition){
console.log('You clicked the next/back button');
}
});
Thanks in advance
You wouldn't need to specifically see whether the next or back button is triggered. 'statechange' is the event that will be triggered when next or back button is pressed. So based on the url change you can manipulate your html content.
Also, statechange is triggered on different occasions as well in different browsers. Chrome for example loads the statechange event as soon as any page loads, even on refresh whereas this is not the case for firefox.
You can keep track of all states (links) in an Array and on
popstate
(orstatechange
) compare the new location to the old one in the array so you will know which way the user went in history (back or fwd).Or you can pass along in the
state obj
(the first param to pushState) a time stamp and compare that to the oldOption 1(has issues - don't use)
This option has a problem that it will get confused if the same link will be in the history more than once
Option 2
PS: I haven't tested if it works, if it doesn't work please make a fiddle and I'l try to fix it