js中的location的href和pathname,search
在我们使用浏览器中访问一个url,HTTP GET获取到一个资源后。我们可以查看浏览器的location对象。
比如,我们search一下,在浏览器中会出现下面的url:
https://www.bing.com/search?q=location.href+location.pathname&go=%E6%8F%90%E4%BA%A4&qs=n&form=QBLH&pq=location.href+location.pathname&sc=0-14&sp=-1&sk=&ghc=1&cvid=be38977d2c4d483ba842e8019853315f
使用window.location.href
得到的结果时浏览器地址栏中的完整地址串:
“https://www.bing.com/search?q=location.href+location.pathname&go=%E6%8F%90%…ion.pathname&sc=0-14&sp=-1&sk=&ghc=1&cvid=be38977d2c4d483ba842e8019853315f”
window.location.pathname
得到URL的路径部分(就是请求映射的地址)
“/search”
如果我们修改pathname,则浏览器中的地址栏url的pathname部分就会改变,并触发新的http get操作。但是后面接着的参数长串依然存在。
window.location.search
得到的是:
“?q=location.href+location.pathname&go=%E6%8F%90%E4%BA%A4&qs=n&form=QBLH&pq=location.href+location.pathname&sc=0-14&sp=-1&sk=&ghc=1&cvid=be38977d2c4d483ba842e8019853315f”
其他:
window.location.host
“www.bing.com”
window.location.hostname
“www.bing.com”
window.location.port
“”
==== 说明: URL 的端口部分
如果采用默认的80端口(update:即使添加了:80),那么返回值并不是默认的80而是空字符
本例返回值:”“
window.location.hash
如果使用了锚点,比如对于这样一个URL
http://localhost:8080/search?ver=1.0&id=6#herepointer
锚点 返回值:#herepointer
---------------------
作者:whereismatrix
来源:CSDN
原文:https://blog.csdn.net/whereismatrix/article/details/47001267
版权声明:本文为博主原创文章,转载请附上博文链接!