在window.popstate上获取新的URL

13

window.popstate 被触发时,有没有办法获取新页面的 URL?

window.onpopstate(function (){
    newPageUrl = //get the new page's URL
})

location.href 不起作用吗? - Alexander O'Mara
window.location.href.toString().split(window.location.host)[‌​1] - Vasim Shaikh
1个回答

23
使用这个
window.onpopstate = function (){
    newPageUrl = location.href;
}

1
这是正确的语法: window.onpopstate = function() {}https://developer.mozilla.org/zh-CN/docs/Web/API/WindowEventHandlers/onpopstate - Marian07
2
基本上,当事件回调被触发时,路由更改已经被注册。因此,currentUrl 已经是 newUrl 了。 - SeanMC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接