learning mark
history.pushState(stateObject, title, url);
tip: stateObject = {key: value, ...}, must be serializable, understood as JSON.stringify & JSON.parse
The received url
parameter is the string
type, which is used to change the url of the current address bar.
One thing to note is that this parameter cannot be the same as cross-domain, that is, the protocol, domain name, and port must be the same
replaceState
The received parameters are the pushState
same, but the final effect is: the address bar url will change according to the received parameters, but the browser does not add the browser's history to the current browsing history stack, but replaces the current browser's history.
Through pushState
and replaceState
can change the URL, but will not actively trigger the browser reload
.
Note: javascript scripts executing window.history.pushState and window.history.replaceState will not trigger onpopstate .
Note: There are differences in how different browsers handle popstate
events when a page is loaded. Chrome and Safari usually emit ( emit ) popstate
the event when the page loads, but Firefox doesn't , so it may be necessary to call it manually once
Note that the pushState() method does not cause the hashchange time to occur, even if the old and new urls are only hashed differently.
https://developer.mozilla.org/zh-CN/docs/Web/Events/popstate