How to check if a back button event has been called?
-
11-12-2019 - |
Question
I am trying to make a site fully ajax supportive however I am having a hard time figuring out how to support history. I am using http://balupton.github.com/jquery-history/demo/ as my hash change library with jquery as such:
$(document).on('click','a',function(){
var url = $(this).attr('href');
$.ajax({
// AJAX Call and populate
});
$.History.go(url);
return false;
});
$('input[type="submit"]').livequery('click',function(event){
$(this).closest("form").ajaxForm(get_pages_load_options);
var url = $(this).closest("form").attr('action');
$.History.go(url);
});
$('.get_pages').livequery('change',function(){
$(this).closest("form").ajaxForm(get_pages_load_options);
var url = $(this).closest("form").attr('action');
$.History.go(url);
});
I am also using the following javascript for bookmarking:
function hash_process() {
url = window.location.hash.replace('#', '');
$.ajax({
//AJAX Call and Populate
});
}
if (window.location.hash) {
hash_process();
}
However with the above two snippets I am finding that the back button does not work, especially with the submitted forms. If I add the following code it just gives me the default pages that do not have any submitted information like I never submitted the form in the first place.
window.onhashchange = hash_process;
Is there a way that I can check if the back button was called? Any ideas would be appreciated, thanks!
Solution
I found the answer to this conundrum. Since the second call was being caused after I submitted the forms I ended up just adding an addition variable into the mix as follows:
ignore_hash = false;
function hash_process() {
if (ignore_hash == false) {
url = window.location.hash.replace('#', '');
$.ajax({
// AJAX Call & Populate
});
} else {
ignore_hash = false;
}
}
In the functions that were initiated before the check I added that variable into the execution:
$(document).on('click','a',function(){
var url = $(this).attr('href');
$.ajax({
// AJAX Call & Populate
});
$.History.go(url);
ignore_hash = true;
return false;
});