Created
October 10, 2019 09:01
-
-
Save Ivannnnn/d66dab04fdf25e49fb792cf93a399199 to your computer and use it in GitHub Desktop.
Subscribe to url change
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const onLocationChange = (() => { | |
const subscribers = [] | |
const fireEvent = () => subscribers.forEach(sub => sub()) | |
const { pushState, replaceState } = window.history | |
window.history.pushState = function(...args) { | |
pushState.apply(window.history, args) | |
fireEvent() | |
} | |
window.history.replaceState = function(...args) { | |
replaceState.apply(window.history, args) | |
fireEvent() | |
} | |
window.addEventListener('popstate', fireEvent) | |
return subscriber => subscribers.push(subscriber) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment