const u = new URL("https://test.co.kr:8000/path?query=value");
u.protocol // "https:"
u.hostname // "test.co.kr"
u.port // "8000"
u.host // "test.co.kr:8000"
u.origin // "https://test.co.kr:8000"
u.pathname // "/path"
u.search // "?query=value"
url형태의 문자열을 넣으면 자동으로 url 구성 요소들 분리 가능.
문자열 정규식보다 간편하고 안전하다.
로컬/배포 환경 차이로 url 구성이 달라지는 경우,
URL 객체를 활용해서 쉽게 비교.
const normalize = (url) => {
const u = new URL(url);
return `${u.protocol}//${u.hostname}`;
};
if (normalize(evenURL) !== normalize(baseURL)) return;
'JavaScript > 공부공부' 카테고리의 다른 글
| [JS / jQuery] 커스텀 select-option 디자인 (0) | 2025.10.18 |
|---|---|
| [JS / jQuery] 동적 요소 이벤트 바인딩 (0) | 2025.07.24 |
| [JS] event.currentTarget vs event.target (0) | 2025.02.27 |
| [JS] Form & Checkbox (2) | 2024.11.06 |
| [JS] Property vs Attribute (0) | 2024.11.06 |