我尝试使用pushState()和popstate()使php和ajax后退按钮工作。我得到pushState以某种方式工作,但popState不是。
for ($i=1; $i<=$lastPage; $i++) {
//$pageLink[] = '<a href="ajax.php?action=merchantlistpage&merchant_id=' . $merchant_id . '&p='.$i.'" onclick="showmerchantitems(' . $merchant_id . ', ' . $i . ');">'.$i.'</a>';
$pageLink[] = '<a href="#p' . $i . '" onclick="showmerchantitems(' . $merchant_id . ', ' . $i . ');">'.$i.'</a>';
};
下面的java脚本。
<script>
function showmerchantitems(str, str2) {
var elements = document.getElementsByClassName("itemwrapper");
if (str.length == 0) {
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "";
}
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = this.responseText;
}
}
};
xmlhttp.open("GET", "ajax.php?action=merchantlistpage&merchant_id=" + str + "&p=" + str2, true);
xmlhttp.send();
history.pushState(null, "", "ajax.php?action=merchantlistpage&merchant_id=" + str + "&p=" + str2);
$(window).bind("popstate", function(){
$.getScript(location.href);
});
}
}
</script>