如何在JavaScript中获取当前URL

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对象,它自己具有许多属性:

window.location

The current page URL is exposed in

当前页面的URL显示在

location.href

Other properties of location provide useful information:

location其他属性提供有用的信息:

CodeDescription
location.hostnamethe host name
location.originthe origin
location.hashthe hash, the part that follow the hash # symbol
location.pathnamethe path
location.portthe port
location.protocolthe protocol
location.searchthe 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值