Get path and query string from URL using javascrip

2019-02-02 02:06发布

I have this:

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

I want this:

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

how do i do this in javascript?

I tried window.location.href but it is giving me whole url
I tried window.location.pathname.substr(1) but it is giving me found-locations/

3条回答
Luminary・发光体
2楼-- · 2019-02-02 02:26

Use location.pathname and location.search:

(location.pathname+location.search).substr(1)
查看更多
别忘想泡老子
3楼-- · 2019-02-02 02:28

URI.js is a nice JavaScript library for parsing URIs. It can do what you requested with a nice fluent syntax.

查看更多
Animai°情兽
4楼-- · 2019-02-02 02:33
window.location.pathname + window.location.search

Will get you the base url /found-locations plus the query string ?state=--&km=km

查看更多
登录 后发表回答