Navigation:entries() 方法

Limited availability

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

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

Navigation 接口的 entries() 方法返回一个 NavigationHistoryEntry 对象数组,代表所有现有的历史记录条目。

语法

js
entries()

参数

无。

返回值

NavigationHistoryEntry 对象数组。

异常

无。

示例

返回历史记录中的条目数

js
let numOfEntries = navigation.entries().length - 1;

智能后退按钮

页面提供的“后退”按钮可以让你返回,即使在重新加载后,也可以通过检查以前的历史记录条目返回:

js
backButtonEl.addEventListener("click", () => {
  if (
    navigation.entries()[navigation.currentEntry.index - 1]?.url ===
    "/product-listing"
  ) {
    navigation.back();
  } else {
    // 如果用户通过其他方式到达此处,例如直接输入 URL:
    navigation.navigate("/product-listing", { history: "replace" });
  }
});

规范

Specification
HTML
# dom-navigation-entries-dev

浏览器兼容性

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
entries
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.

参见