使用JS获取当前页面的URL(网址信息)

JS获取URL各部分详解
本文详细解析了如何使用JavaScript获取URL的各个组成部分,包括完整URL、文件路径、协议、主机地址、端口号、锚点及属性等。通过具体代码示例,读者可以了解并掌握在网页开发中如何有效利用这些信息。
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<script>
			var url;
 
			url = window.location.href; /* 获取完整URL */
			alert(url); /* http://127.0.0.1:8020/Test/index.html#test?name=test */
 
			url = window.location.pathname; /* 获取文件路径(文件地址) */
			alert(url); /* /Test/index.html */
 
			url = window.location.protocol; /* 获取协议 */
			alert(url); /* http */
 
			url = window.location.host; /* 获取主机地址和端口号 */
			alert(url); /* http://127.0.0.1:8020/ */
 
			url = window.location.hostname; /* 获取主机地址 */
			alert(url); /* http://127.0.0.1/ */
 
			url = window.location.port; /* 获取端口号 */
			alert(url); /* 8020 */
 
			url = window.location.hash; /* 获取锚点(“#”后面的分段) */
			alert(url); /* #test?name=test */
 
			url = window.location.search; /* 获取属性(“?”后面的分段) */
			alert(url);
 
			/* 如果需要URL中的某一部分,可以自己进行处理 */
			url = window.location.pathname;
			url = url.substring(url.lastIndexOf('/') + 1, url.length);
			alert(url); /* /index.html */
 
			/* 
			 * 如果页面使用了框架(frameset)
			 * 要获取到指定页面的URL
			 * 只要把window换成指定的页面即可
			 */
			/* 'frame'为指定页面的class名 */
			var url = window.parent.frames['frame'].location.href;
			/* 获取当前地址栏中显示的URL */
			var url = window.parent.location.href;
			/* window parent 可互换 */
			var url = parent.window.location.href;
		</script>
	</head>
	<body>
	</body>
</html>

 

<think> 我们之前讨论的是`history.replaceState()`方法,但现在用户询问的是如何获取当前页面URL。 根据用户当前的问题:“我想查找使用JavaScript获取当前页面URL的方法”,我们需要提供获取当前URL的几种常用方式。 注意:用户可能是在浏览器环境中,所以我们可以使用`window.location`对象。 常见的获取当前页面URL的方法: 1. `window.location.href`:返回完整的URL(字符串) 2. `window.location.toString()`:同样返回完整的URL 3. `document.URL`:返回文档的URL(只读) 另外,我们还可以获取URL的各个组成部分,例如: - 协议:`window.location.protocol` - 主机:`window.location.host` - 路径:`window.location.pathname` - 查询参数:`window.location.search` - 哈希:`window.location.hash` 但是用户的问题明确是获取整个URL,所以重点放在完整URL上。 另外,注意:在浏览器中,以上属性都是可用的。在非浏览器环境(如Node.js)中,这些属性不存在。 因此,我们可以这样回答: </think> ### JavaScript 获取当前页面 URL 的方法 在 JavaScript 中,获取当前页面 URL 主要通过 `window.location` 对象实现,以下是常用的方法和属性: #### 1. 获取完整 URL(字符串形式) ```javascript const currentURL = window.location.href; console.log(currentURL); // 输出: "https://example.com/path?query=1#section" ``` - `location.href` 返回完整的 URL 字符串[^1] - 最常用方法,适用于需要完整 URL 的场景 #### 2. 获取 URL 各组成部分 ```javascript // URL 结构: https://example.com:8080/path?query=1#section console.log(window.location.protocol); // "https:" console.log(window.location.host); // "example.com:8080" console.log(window.location.hostname); // "example.com"(不含端口) console.log(window.location.port); // "8080" console.log(window.location.pathname); // "/path" console.log(window.location.search); // "?query=1" console.log(window.location.hash); // "#section" console.log(window.location.origin); // "https://example.com:8080"(协议+主机+端口) ``` #### 3. 其他获取方式 ```javascript // 等效于 location.href const url = window.location.toString(); // 通过 document 对象获取(只读) const docURL = document.URL; ``` #### 特殊场景处理 1. **获取无哈希/参数的纯净 URL** ```javascript const cleanURL = location.origin + location.pathname; ``` 2. **解析查询参数** ```javascript const params = new URLSearchParams(location.search); console.log(params.get("query")); // 输出 "1" ``` 3. **动态 URL 更新监听** ```javascript window.addEventListener("popstate", () => { console.log("URL变化:", location.href); }); ``` #### 注意事项 - 所有属性均为**只读**(修改需用 `history.pushState()` 或 `location.assign()`) - 在 Web Workers 中不可用(需通过 `postMessage` 传递) - 本地文件协议(`file://`)中 `location.origin` 返回 `"null"`[^2] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值