i had a problem about preventing ajax request on Popstate event.
so, this is my code :
$(window).on('popstate', function(e) {
var state = e.originalEvent.state;
if (state !== null) {
loadProducts(location.pathname);
}
});
function loadProducts(url) {
$.ajax({
type : "GET",
url : url,
dataType : "html",
cache : true,
success : function(checks) {
$(".ajaxpage").html(checks);
}
});
};
$('#product').on("click", ".paging a", function(e) {
e.preventDefault();
history.pushState(null, null, $(this).attr('href'));
loadProducts($(this).attr('href'));
});
pushstate event works really well with ajax request. but when i hit "back button" the page will do the same ajax request again. why is that? i don't need ajax anymore cause i already visit that page? just like what Github does?
how to prevent that ajax request on popstate event?
what's wrong with my code?
This line is wrong
var state = e.originalEvent.state
You should access the state by writing
var state = e.state
For reference, https://developer.mozilla.org/en-US/docs/Web/Events/popstate
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments