NavigationDestination:url 属性

Limited availability

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

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

NavigationDestination 接口的 url 只读属性返回导航目标的 URL。

字符串。

示例

使用 intercept() 处理导航

js
navigation.addEventListener("navigate", (event) => {
  // 如果此导航不应被拦截,则提前退出,例如,如果导航是跨源的,或者是下载请求
  if (shouldNotIntercept(event)) {
    return;
  }

  const url = new URL(event.destination.url);

  if (url.pathname.startsWith("/articles/")) {
    event.intercept({
      async handler() {
        // URL 已更改,因此在获取新内容时显示占位符,例如旋转器或加载页面
        renderArticlePagePlaceholder();

        // 获取新内容并在准备就绪时显示
        const articleContent = await getArticleContent(url.pathname);
        renderArticlePage(articleContent);
      },
    });
  }
});

规范

Specification
HTML
# the-navigationdestination-interface:dom-navigationdestination-url-2

浏览器兼容性

BCD tables only load in the browser

参见