I'am trying to create a website where all pages are called with Ajax. When I click on any link on the page, the url and page content changes without refreshing all page (similar Facebook). So far everything is fine.
The problem is : When I click the go back or go forward button, the url changes but the page content does not change.
Example code :
function loadDoc($link) { // example for $link : /example.php
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var parser = new DOMParser();
var doc = parser.parseFromString(xhttp.responseText, "text/html");
var elem = doc.getElementById("content");
document.getElementById("content").innerHTML = elem.innerHTML;
}
};
xhttp.open("GET", $link, true);
xhttp.send();
window.history.pushState('', 'Settings', $link); // dynamic url changing
}
(Fully separate structure or additional ideas are welcomed too)