Popstate - 将弹出状态传递给事件处理程序 [英] Popstate - passing popped state to event handler

查看:104
本文介绍了Popstate - 将弹出状态传递给事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码应该会引发1警告,但不会做任何事情。

The following code should cause an alert of '1', but instead does nothing.

window.onpopstate = function(event) { alert(event.state.a) }
history.pushState({a: 1})
history.back()

小提琴: http://jsfiddle.net/WNurW/ 2 /

任何想法?

推荐答案

您的代码不会导致popstate,因为pushstate命令告诉你现在在哪个页面。

Your code woudn't cause a popstate, as the pushstate command tells what page you are on NOW.

window.onpopstate = function(event) { alert(event.state.a) }
history.pushState({a: 1});
history.pushState({a: 2});
history.back()

以上代码可以使用。

继承人: http://jsfiddle.net/WNurW/8/

正如您在上面的图片中看到的那样:

(1)在这里您输入了页面或小提琴,然后您想要pushState,这将添加一个新的链接到历史链。

As you can see on the above picture:
(1) Here you entered the page, or the fiddle, you then want to pushState, which will add a new link to the history chain.

(2)当您按下状态时,您将再添加一次回溯到历史记录,但它也会移动当前位置历史直到你的新国家。所以回去,不会给你你认为你得到的历史状态,它会给你前一个。

(2) When you push state, you will add one more back click to the history, but it will also move the current place in "history" up to your new state. So going back, will not give you the history state you think you are getting, it will give the previous one.

(3)您必须转到新页面,或推送其他历史状态,才能返回您在步骤(2)中创建的状态。

(3) You have to go to a "new" page, or push another history state, to be able to go back to the state you created in step (2).

这篇关于Popstate - 将弹出状态传递给事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆