得到URL使用javascript路径和查询字符串(Get path and query strin

2019-09-01 23:50发布

我有这个:

http://127.0.0.1:8000/found-locations/?state=--&km=km

我要这个:

found-locations/?state=--&km=km

我如何做到这一点在JavaScript?

我试图window.location.href但它给我的整个URL
我试图window.location.pathname.substr(1)但它给我found-locations/

Answer 1:

使用location.pathnamelocation.search

(location.pathname+location.search).substr(1)


Answer 2:

window.location.pathname + window.location.search

将让你的基础URL /found-locations以及查询字符串?state=--&km=km



Answer 3:

URI.js是一个很好的JavaScript库解析URI的。 它可以做什么,你有一个很好的流畅的语法要求。



文章来源: Get path and query string from URL using javascript