当前位置: 首页 > Web前端 > HTML

关于地址栏中url的一些总结

时间:2023-04-02 17:08:19 HTML

1.Gettheentireaddressbaraddress//获取整个地址栏地址varhref=window.location.href;控制台日志(href);上面代码是获取整个url地址2.Gettheurlprotocolpart//获取url协议部分varprotocol=window.location.protocol;控制台日志(协议);如果url为http://www.baidu.com,则window.location.protocol为http:3.Getthehostpart//获取host部分(带端口号)varhost=window.location.host;控制台日志(主机);如果url是http://www.baidu.com/test.htm...,或者url地址是http://192.1.1.1:1111/test/test.html,那么window.location.host就是192.1.1.1:1111如果有端口号,也要包含端口号//获取主机部分(不带端口号)varhostname=window.location.主机名;控制台日志(主机名);url地址是http://192.1.1.1:1111/test/test.html,那么主机名是192.1.1.14。获取端口号//获取端口号varport=window.location.港口;控制台日志(端口);url地址为http://192.1.1.1:1111/test/test.html,则window.location.port为11115Gettheurlpartpath//获取部分路径varpathname=window.location.pathname;控制台日志(路径名);url地址是http://192.1.1.1:1111/test/test.html?a=1,那么window.location.pathname就是/test/test.html,也就是host部分后面的部分到前面参数部分的是pathname6.获取url参数部分//参数部分varsearch=window.location.search;控制台日志(搜索);url地址是http://192.1.1.1:1111/test/test.html?a=1&b=2,那么搜索值是?a=1&b=27。获取锚//获取锚varhash=window.location.hash;控制台日志(哈希);url地址是http://192.1.1.1:1111/test/test.html?a=1&b=2#1,那么hash是#1