Navigation:currententrychange 事件

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

实验性: 这是一项实验性技术
在将其用于生产之前,请仔细检查浏览器兼容性表格

Navigation 接口的 currententrychange 事件在 Navigation.currentEntry 发生更改时触发。

此事件将会在如下情况发生时触发:

此事件在导航提交后触发,这意味着可见 URL 已更改且 NavigationHistoryEntry 已更新。它对于从使用较旧的 API 功能(如 hashchange 事件或 popstate 事件)进行迁移非常有用。

语法

在类似 addEventListener() 这样的方法中使用事件名称,或者设置事件处理器属性。

js
addEventListener("currententrychange", (event) => {});

oncurrententrychange = (event) => {};

事件类型

示例

导航数据上报:

js
navigation.addEventListener("currententrychange", () => {
  const data = navigation.currentEntry.getState();
  submitAnalyticsData(data.analytics);
});

设置每个条目事件:

js
navigation.addEventListener("currententrychange", () => {
  navigation.currentEntry.addEventListener("dispose", genericDisposeHandler);
});

规范

Specification
HTML
# event-currententrychange

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
currententrychange event
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.
See implementation notes.

参见