Javascript路径名引用者

Mat*_*att 5 javascript location referrer pathname

如何在javascript中使用document.location.pathname中的相同功能 - 除了引用者?像document.referrer.pathname这样的东西?

谢谢.

RRe*_*ser 6

不,您只能手动提取所需的部分:

document.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '')
Run Code Online (Sandbox Code Playgroud)


hal*_*ube 5

您可以从 中提取路径名document.referrernew URL()使用以下代码对其进行解析

const url = new URL(document.referrer)
url.pathname
Run Code Online (Sandbox Code Playgroud)

确保为 IE 10 及以下版本填充 URL,使用https://polyfill.io/v3/polyfill.js?features=URL轻松完成