Javascript URLSearchParams

이승훈·2025년 10월 30일

주소에서 가져오기

//하나의 키에 하나의 값
function searchParam(key) {
  return new URLSearchParams(location.search).get(key);
};

//하나의 키에 여러개의 값
function searchParam(key) {
  return new URLSearchParams(location.search).getAll(key);
};

문자열에서 가져오기

const url = "https://www.example.com?name=lee";

const parsedUrl = new URL(url);
const category = parsedUrl.searchParams.get("lee");

console.log(category); // "lee"
profile
안녕하세요!

0개의 댓글