반응형
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/
Use location.pathname
and location.search
:
(location.pathname+location.search).substr(1)
window.location.pathname + window.location.search
Will get you the base url /found-locations
plus the query string ?state=--&km=km
URI.js is a nice JavaScript library for parsing URIs. It can do what you requested with a nice fluent syntax.
If your url is a string, you can create URL
object and use pathname
and search
property.
let strurl = 'http://www.test.com/param1/param2?test=abc';
let url = new URL(strurl)
let pathandQuery = url.pathname + url.search;
let strurl = 'http://www.test.com/param1/param2?test=abc';
let url = new URL(strurl)
let pathandQuery = url.pathname + url.search;
console.log(pathandQuery);
ReferenceURL : https://stackoverflow.com/questions/16376438/get-path-and-query-string-from-url-using-javascript
반응형
'Programing' 카테고리의 다른 글
HTML 스크립트 태그에서 비동기 및 지연 속성을 모두 사용할 수 있습니까? (0) | 2020.12.15 |
---|---|
PostgreSQL에서 외래 키가있는 행 삭제 (0) | 2020.12.15 |
PHPStorm 새 파일 브랜딩 비활성화 (0) | 2020.12.15 |
Pandas에서 datetime 형식을 변경하는 방법 (0) | 2020.12.15 |
람다를 이해하려고 (0) | 2020.12.15 |