在JavaScript中,如何获取完整的路径,包括尾随英镑之后的数据?

问题描述 投票:-1回答:2

我的完整URL是

/settings/logs#/extra

但是window.location.pathname刚刚返回

/settings/logs

在浏览器中,如何获取包括#和其后信息的完整路径?

javascript
2个回答
1
投票
如果您只需要“路径”,则可以使用window.location.href获取完整的url(带有协议,主机等),或者将window.location.pathnamewindow.location.hash并置。

console.log(window.location.href); // Result: https://www.google.com/path/#hash console.log(window.location.pathname + window.location.hash); // Result: /path/#hash

请参见Location API

0
投票
尝试window.location.href有关更多信息:

https://www.w3docs.com/snippets/javascript/how-to-get-current-url-in-javascript.html

© www.soinside.com 2019 - 2024. All rights reserved.