javascript 历史 onpopstate
我正在尝试理解 HTML5 历史对象。这是我开始时的一个简单示例。
function addDialog(){
document.getElementById('d').style.display ='';
history.pushState({name:"changed"},"","#newURL");
}
window.onpopstate = function(e){
alert(e.state);
}
我有一个 id 为 d
的 div
,其 display
属性为 none
。单击链接时,我将显示 div 并更改历史记录,以便加载新的 url。
当我复制粘贴新网址时,popstate
事件被触发,并且我得到 e.state
的 null
。
据我了解,如果我加载新网址 http://example.com#newURL
,e.state
应该指向我使用 Pushstate 推送的对象。
如果我错了,请纠正我,我也想知道 e.state
何时被填充。
I am trying to understand HTML5 history object. Here is a simple example which I started off with.
function addDialog(){
document.getElementById('d').style.display ='';
history.pushState({name:"changed"},"","#newURL");
}
window.onpopstate = function(e){
alert(e.state);
}
I have a div
with an id d
for which display
property is none
. On clicking a link, I will display the div and change the history so that new url will be loaded.
When I copy paste the new url, popstate
event is fired and I get null
for e.state
.
From what I understand, if I load the new url http://example.com#newURL
, e.state
should point to the object which I pushed using pushstate.
Please correct me if I am wrong and also I would like to know when e.state
gets populated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我测试的,e.state 仅获取当您单击“后退”或“前进”按钮时添加到历史记录中的弹出状态。否则,它会给你 null。
您可以对 URL 使用参数,以便测试请求是来自历史记录调用还是来自地址栏中的 URL。
As I tested, the e.state only get the pop state that you add to the history when you click in the Back or Forward button. Otherwise, it will give you the null.
You can use parameters to your URL so you can test if the request came from a history call or from an URL in the location bar.