作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我使用 Swift 开发了混合 iOS 应用程序并希望在 WKWebView 中检测 history.pushstate()。我确实覆盖了 WKWebView 的方法,但我无法检测到任何东西。
有没有办法或技巧来检测 history.pushstate()
最佳答案
这当然是一种解决方法,但如果允许您编辑内置对象的属性(就像在大多数浏览器中一样),您可以包装这两个函数并使用拦截处理程序来跟踪它们何时被调用:
function track (fn, handler) {
return function interceptor () {
handler.apply(this, arguments)
return fn.apply(this, arguments)
}
}
history.pushState = track(history.pushState, function (state, title, url) {
// do something with `state, `title`, and `url`
console.log('pushState called with:', { state: state, title: title, url: url })
})
history.replaceState = track(history.replaceState, function (state, title, url) {
// do something with `state, `title`, and `url`
console.log('replaceState called with:', { state: state, title: title, url: url })
})
history.pushState(null, 'Title 1', '/example-page')
关于javascript - 如何在 WKWebView 中检测 history.pushstate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42476474/
我是一名优秀的程序员,十分优秀!