如何在JavaScript中执行与document.location.pathname相同的功能,但使用referrer?因此类似于document.referrer.pathname?
谢谢。
如何在JavaScript中执行与document.location.pathname相同的功能,但使用referrer?因此类似于document.referrer.pathname?
谢谢。
document.referrer
中提取路径名并使用new URL()
进行解析:const url = new URL(document.referrer)
url.pathname
请务必为IE 10及以下版本使用URL polyfill,可以轻松地通过https://polyfill.io/v3/polyfill.js?features=URL实现。
new URL(document.referrer, "http://example.com/")
。 - Wilco Verhoefdocument.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '')
document.location.pathname
也会剥离 URL 的参数。你需要使用:
document.referrer.replace(/^[^:]+:\/\/[^/]+/, '').replace(/#.*/, '').replace(/\?.*/, '')
来同时删除引荐者的参数。 - clement gdocument.referrer
获取引用文档的URL。这是你指的吗?