HTML URL地址解析

通过JavaScript的location对象,可获取URL中的协议、主机名、端口、锚点、查询参数等信息。

示例

URLhttp://www.akmsg.com/WebDemo/URLParsing.html#top?username=admin&pwd=123456

解析结果: 

属性名称获取的值说明
location.hash#top?username=admin&pwd=123456URL中的的锚点部分,包含开头的#符号
location.hostwww.akmsg.com主机名称和端口
location.hostnamewww.akmsg.com主机名称
location.hrefhttp://www.akmsg.com/WebDemo/URLParsing.html#top?username=admin&pwd=123456完整的URL
location.pathname/WebDemo/URLParsing.html路径部分
location.port 端口
location.protocolhttp:协议,最后面会有个':'冒号
location.search URL的查询部分(从问号 (?) 开始的 URL)
注意:当URL含有锚点时,此处返回空字符。
location.originhttp://www.akmsg.comURL的源。返回格式:协议+主机名+端口
location.origin+location.pathnamehttp://www.akmsg.com/WebDemo/URLParsing.htmlURL的访问地址。返回格式:协议+主机名+端口+路径部分

代码

console.log(
	'location.hash     :' + location.hash     + '\r\n' +
	'location.host     :' + location.host     + '\r\n' +
	'location.hostname :' + location.hostname + '\r\n' +
	'location.href     :' + location.href     + '\r\n' +
	'location.pathname :' + location.pathname + '\r\n' +
	'location.port     :' + location.port     + '\r\n' +
	'location.protocol :' + location.protocol + '\r\n' +
	'location.hash     :' + location.hash     + '\r\n' +
	'location.search   :' + location.search   + '\r\n' +
	'location.origin   :' + location.origin 
)

 

在线示例

地址http://www.akmsg.com/WebDemo/URLParsing.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值