Skip to content
Advertisement

Get path and query string from URL using javascript

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/

Advertisement

Answer

Use location.pathname and location.search:

(location.pathname+location.search).substr(1)
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement