location.href/location.search/location.pathname区别及用法

location.href/location.search/location.pathname区别及用法:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>location.href/location.search/location.pathname区别及用法</title>
	</head>
	<body>
		<script type="text/javascript">
			alert(location.href);
			alert(location.pathname);
		</script>
	</body>
</html>

location.href: 返回完整URL。

location.pathname: 返回URL的域名(域名IP)后的部分。

既然提到这了,那我们就分析下下面的URL:

http://www.example.com:8080/test.php?user=admin&pwd=admin#login

想得到整个如上的完整url,我们用:location.href; 
得到传输协议http:,我们用:location.protocol; 
得到主机名连同端口www.example.com:8080,我们用:location.host; 
得到主机名www.joymood.cn,我们用:location.hostname; 
得到主机后部分不包括问号?后部分的/test.php,就用我们刚才讲的:location.pathname; 
得到url中问号?之后井号#之前的部分?user=admin&pwd=admin,我们就用: location.search; 
得到#之前的部分#login,我们就用location.hash

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值