To get the current URL of the page you opened in the browser using JavaScript you can rely on the location
property exposed by the browser on the window
object:
要获取使用JavaScript在浏览器中打开的页面的当前URL,可以依赖浏览器在window
对象上显示的location
属性:
window.location
Since window
is the global object in the browser, the property can be referenced as
由于window
是浏览器中的全局对象,因此该属性可以引用为
location
This is a Location object which has many properties on its own:
这是一个Location对象,它自己具有许多属性:
The current page URL is exposed in
当前页面的URL显示在
location.href
Other properties of location
provide useful information:
location
其他属性提供有用的信息:
Code | Description |
---|---|
location.hostname | the host name |
location.origin | the origin |
location.hash | the hash, the part that follow the hash # symbol |
location.pathname | the path |
location.port | the port |
location.protocol | the protocol |
location.search | the query string |
码 | 描述 |
---|---|
location.hostname | 主机名 |
location.origin | 起源 |
location.hash | 井号,井号#符号后的部分 |
location.pathname | 路径 |
location.port | 港口 |
location.protocol | 协议 |
location.search | 查询字符串 |
翻译自: https://flaviocopes.com/how-to-get-current-url-javascript/