diff -u b/misc/ajax_facets.js b/misc/ajax_facets.js --- b/misc/ajax_facets.js +++ b/misc/ajax_facets.js @@ -365,7 +365,7 @@ facets: Drupal.ajax_facets.queryState['f'] }; - History.pushState(state, document.title, stateUrl); + Drupal.ajax_facets.pushState(state, document.title, stateUrl); } //Pass back to original method. @@ -530,7 +530,7 @@ */ Drupal.ajax_facets.initHistoryState = function ($facet) { // Set the initial state only initial page load. - if (typeof history.replaceState != "undefined" && Drupal.ajax_facets.firstLoad) { + if (Drupal.ajax_facets.firstLoad) { History.replaceState({ current_id: $facet.attr('id'), current_facet_name: $facet.data('facet'), @@ -543,10 +543,22 @@ /** - * HTML5 popstate callback. Re-runs the ajax query based on stored state info. + * Pushes new state to browser history. + * + * History.js library fires "statechange" event even on API push/replace calls. + * So before pushing new state to history we should unbind from this event and after bin again. */ - History.Adapter.bind(window, 'statechange', function() { + Drupal.ajax_facets.pushState = function (state, title, stateUrl) { + $(window).unbind('statechange', Drupal.ajax_facets.reactOnStateChange); + History.pushState(state, title, stateUrl); + $(window).bind('statechange', Drupal.ajax_facets.reactOnStateChange); + }; + + /** + * Callback for back/forward browser buttons. + */ + Drupal.ajax_facets.reactOnStateChange = function () { var state = History.getState(); Drupal.ajax_facets.queryState['f'] = state.data.facets; Drupal.ajax_facets.sendAjaxQuery($('#' + state.data.current_id), false); - }); + }; })(jQuery);