LoginSignup
1
0

More than 5 years have passed since last update.

Implement onurlchange Event

Posted at

Even if modifying build-in functions isn't good hack...

function insertOnUrlChange(parent, funcname) {
    let oldFunc = parent[funcname] ? parent[funcname] : function (){}
    parent[funcname] = function() {
        oldFunc.apply(this, arguments)
        return window.onurlchange()
    }
}

insertOnUrlChange(window.history, "pushState")
insertOnUrlChange(window.history, "replaceState")

window.onurlchange = function() {
    doSomething()
}

window.history.pushState(null, null, "/hoge#foo=bar") // doSomething()
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0