Get path and query string from URL using javascript

doniyor picture doniyor · May 4, 2013 · Viewed 84.1k times · Source

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/

Answer

Gumbo picture Gumbo · May 4, 2013

Use location.pathname and location.search:

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