关于地址栏url的一些小结

1.获取整个地址栏地址

 //获取整个地址栏地址
     var href = window.location.href;
     console.log(href);

以上代码就是获取整个url地址

2.获取url协议部分

 //获取url协议部分
     var protocol = window.location.protocol;
     console.log(protocol);

如果url为http://www.baidu.com,则window.location.protocol就是http:

3.获取主机部分

  //获取主机部分(带端口号)
    var host = window.location.host;
    console.log(host);

如果url为http://www.baidu.com/test.html,则window.location.host为www.baidu.com,或者url地址为http://192.1.1.1:1111/test/test.html,则window.location.host为192.1.1.1:1111如果有端口号,端口号也是要带上的

  //获取主机部分(不带端口号)
    var hostname = window.location.hostname;
    console.log(hostname);

url地址为http://192.1.1.1:1111/test/test.html,则hostname为192.1.1.1

4.获取端口号

  //获取端口号
    var port = window.location.port;
    console.log(port);

url地址为http://192.1.1.1:1111/test/test.html,则window.location.port为1111

5.获取url部分路径

//获取部分路径
    var pathname = window.location.pathname;
    console.log(pathname);

url地址为http://192.1.1.1:1111/test/test.html?a=1,则window.location.pathname为/test/test.html,也就是主机部分后面到参数部分前面的就是pathname

6.获取url参数部分

//参数部分
    var search = window.location.search;
    console.log(search);

url地址为http://192.1.1.1:1111/test/test.html?a=1&b=2,则search值为?a=1&b=2

7.获取锚点

  //获取锚点
    var hash = window.location.hash;
    console.log(hash);

url地址为http://192.1.1.1:1111/test/test.html?a=1&b=2#1,则hash为#1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值