Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the pushState event

Shulou Source: shulou.com Published: 2022-06-03 11:02:15 10月03日 Update

This article mainly introduces how to get pushState events, the article is very detailed, has a certain reference value, interested friends must read it!

History.pushState and history.replaceState can change the URL without refreshing the current page, but it is not possible to get the content of the new page obtained through AJAX.

Pushstate event message

Although various HTML5 documents say that the _ window.onpopstate event can intercept messages from pushState, in the actual test, onpopstate has no effect at all and cannot intercept messages from pushState.

After some Google, I found the code to get the pushState event correctly.

Get the code for the pushState event correctly

/ / Addthis:

Var_wr=function (type) {

Varorig=history [type]

Returnfunction () {

Varrv=orig.apply (this,arguments)

Vare=newEvent (type)

E.arguments=arguments

Window.dispatchEvent (e)

Returnrv

}

}

History.pushState=_wr ('pushState')

History.replaceState=_wr ('replaceState')

/ / Useitlikethis:

Window.addEventListener ('pushState',function (e) {

Console.warn ('THEYDIDITA GAING')

})

Window.addEventListener ('replaceState',function (e) {

Console.warn ('THEYDIDITA GAING')

})

This code rewrites the original function in history and then activates an event itself

This can solve the problem that pushState cannot activate events.

Also remember that it is best to execute this code before the document is loaded.

The above is all the content of the article "how to get pushState events". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Events code content messages documents activation articles pages values functions interests functions actual guys buddies situations more best fundamental knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei NVidia MySQL MariaDB Linux