从 window.location 获取当前 URL 的文本片段部分

Get the text fragment part of current URL from window.location

我想为使用 text fragment link.

访问我网站的任何人提供自定义布局

例如。 https://en.wikipedia.org/w/index.php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats

此处:~:之后的部分将是文本片段。我想得到那个部分。

我试过 window.location.hrefwindow.location.hash。但 window.location.href returns 只是 url 没有 text fragmentwindow.location.hash returns 是一个空字符串。

要复制此访问 https://en.wikipedia.org/w/index.php?title=Cat&oldid=916388819#:~:text=Claws-,Like%20almost,the%20Felidae%2C,-cats,请将其复制并粘贴到新选项卡,然后尝试从 window.location 获取文本片段(markdown link 文本片段似乎有问题).

那么有没有其他方法可以获取地址栏中的文本片段部分?

您可以通过 performance.getEntriesByType("navigation")[0].name 获取文本片段(您可以 运行 一个正则表达式来获取实际值,例如 performance.getEntriesByType("navigation")[0].name.split('#:~:text=')[1] [但您可能想让它更宽容和防错]).