I am trying to implement an HTML5 story with an AJAX form.
The form contains some radio buttons and drop-down menus. After changing any of these inputs, the form is automatically submitted and the results are returned via AJAX.
Now, having implemented the story, the URL is updated, so it looks like this:
/currencies?type=usd&year=2015
This is how I execute AJAX and update the url:
$('#currency-form input, #currency-form select').change(function() {
var form = $('#currency-form');
var url = form.attr('action');
var data = form.serialize();
$.ajax({
url: url,
type: 'get',
dataType: 'json',
data: data,
success: function(response) {
processResponse(response.data);
window.history.pushState({}, response.meta_title, response.new_url);
}
});
});
To find the back button, I did the following:
$(window).on('popstate', function(event) {
var state = event.originalEvent.state;
if (state !== null) {
console.log(state);
}
});
There is one thing I'm struggling with. By clicking the "Back" button, he must first select the previous form values and submit the form again.
Does anyone know how I can achieve this?
Gstar source
share