简体   繁体   中英

How to use replaceState() instead of pushState()

I have implemented History.js with History.pushState fuction. Here is code below:

function PushStateUrl(stateUrl) {
  manualStateChange = false;
  History.pushState({ loadUrl: stateUrl, rand: Math.random() }, document.title, stateUrl);
}

And on browser's back button, this code executes:

$(function () {
 History.Adapter.bind(window, "statechange", function () {
 var State = History.getState();
 var url = ((State.data.loadUrl == "undefined") ? State.data.loadUrl : State.url);
 if (typeof (manualStateChange) !== "undefined" && manualStateChange == true) {
    AjaxCall(url);
}
else if (typeof (State.data.loadUrl) == "undefined") {
    AjaxCall(url);
}
 manualStateChange = true;
 });    
});

But I want to use replaceState() instead of pushState() . Please guide me to what changes needs to use history.replaceState()

But I want to use replaceState() instead of pushState(). Please guide me to what changes needs to use history.replaceState()

Change the word push to replace in your code. The two functions take the same arguments.

Obviously, if you do that everywhere , then your code for handling the back button becomes redundant (because you are never going to be going back to a stored state).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM