Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get the pushState event

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report