Intercept pushstate and replacestate events via JS

Source: Internet
Author: User

History.pushstate and history.replacestate can change the URL without refreshing the current page, but this will not get the content of the new page that gets through Ajax.
Although various HTML5 documents say that the Window.onpopstate event can intercept pushstate messages, in the actual test, Onpopstate has no effect at all and cannot intercept pushstate messages.

After Google, we found the code to get the Pushstate event correctly.
https://stackoverflow.com/a/25673911

//ADD this:var_WR =function(type) {varOrig =History[type]; return function() {        varRV = orig.apply ( This, arguments); varE =NewEvent (type); E.arguments=arguments;        Window.dispatchevent (e); returnRV; };}; History.pushstate= _WR (' pushstate '); History.replacestate= _WR (' replacestate ');//Use it like this :Window.addeventlistener (' Pushstate ',function(e) {Console.warn (' They did IT again! ');}); Window.addeventlistener (' Replacestate ',function(e) {Console.warn (' They did IT again! ');});

This code overwrites the original function in the history and then activates an event yourself
This will solve the problem that pushstate cannot activate the event.
Also remember that it is best to put this code before the document is loaded to execute

Intercepting Pushstate and replacestate events via JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.