在JavaScript中,可以通过window.location对象来获取当前页面的URL信息。以下是一些常用属性: 1. 获取完整URL: ```javascript var url = window.location.href; ``` 2. 获取协议(http或https): ```javascript var protocol = window.location.protocol; ``` 3. 获取主机名(包括端口号): ```javascript var hostname = window.location.hostname; ``` 4. 获取端口号: ```javascript var port = window.location.port; ``` 5. 获取路径名: ```javascript var pathname = window.location.pathname; ``` 6. 获取查询字符串(URL中问号后面的部分): ```javascript var search = window.location.search; ``` 例如,如果你的网址是 `https://www.example.com:8080/pathname?query=string`,那么上述变量将分别包含: - url: `"https://www.example.com:8080/pathname?query=string"` - protocol: `"https:"` - hostname: `"www.example.com"` - port: `"8080"` - pathname: `"/pathname"` - search: `"?query=string"` [2024-01-22 17:48:07 | AI写代码神器 | 240点数解答]