
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人
2.5.3 browserHistory事件处理
在browserHistory中,监听popstate事件用以获知浏览器地址的改变,如调用history.go,则单击浏览器的“前进”和“后退”按钮,通过window.location.hash改变hash等都会触发popstate事件。在popstate事件中,browserHistory会更新其维护的地址信息并通知history的监听者。注意,history.pushState、history.replaceState并不会触发popstate事件。在源码中,browserHistory在needsHashChangeListener为true时监听了hashchange事件,源码如下:

在功能职责上,browserHistory应该对URL的所有变化做出反应。相对于Chrome等浏览器hash变化会触发popstate事件,在IE浏览器上,改变hash不会触发popstate事件,而会触发hashchange事件。因此,在IE浏览器上,即便是browserHistory,也需要对hashchange事件进行监听,以应对URL的所有变化。