On two different pages I'm trying to change the URL without reloading the page. I also need the forward and back browser buttons to go to the original URL, not the new URL.
I created a switch to determine if I am on one of the pages which needs to change the URL.
And then I put together a snippet for changing the URL based on this this answer. But this is where I'm stuck.
Because I'm not sure how I should be calling processAjaxData
, which I assume is where I pass in the new URL slug. Also what should I be passing in for response
?
<script>
var windowLoc = jQuery(location).attr('pathname'); //jquery format to get window.location.pathname
switch (windowLoc) {
case "/the-old-url-I-want-to-change/":
function processAjaxData(response, urlPath) {
document.getElementByClass("page").innerHTML = response.html;
document.title = response.pageTitle;
window.history.pushState({"html": response.html, "pageTitle": response.pageTitle}, "", urlPath);
}
window.onpopstate = function(e) {
if (e.state) {
document.getElementByClass("page").innerHTML = e.state.html;
document.title = e.state.pageTitle;
}
};
break;
case "/another-old-url-I-want-to-change/":
function processAjaxData(response, urlPath) {
document.getElementByClass("page").innerHTML = response.html;
document.title = response.pageTitle;
window.history.pushState({"html": response.html, "pageTitle": response.pageTitle}, "", urlPath);
}
window.onpopstate = function(e) {
if (e.state) {
document.getElementByClass("page").innerHTML = e.state.html;
document.title = e.state.pageTitle;
}
};
break;
}
</script>