js获取地址栏请求路径【转】

在我们使用浏览器中访问一个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”

原文地址:https://blog.csdn.net/whereismatrix/article/details/47001267

猜你喜欢

转载自blog.csdn.net/nicolas_apahce/article/details/81099188