반응형
Javascript를 사용해 현재 페이지의 URL을 가져오는 방법
예시 코드) React, js에서 현재 페이지 URL 관련 정보를 가져오는 방법
현재 페이지의 URL을 http://dev.kfdd6630.net:3000/blog/post?page=1 라고 가정하자.
1. window.location.href = 현재 페이지의 href (URL) 반환한다.
console.log(window.location.href);
출력값
http://dev.kfdd6630.net:3000/blog/post?page=1
2. window.location.host = 호스트의 도메인 네임 / 포트 번호 반환한다.
console.log(window.location.host);
출력값
dev.kfdd6630.net:3000
3. window.location.hostname = 웹 호스트의 도메인 네임 반환한다.(ex > localhost)
console.log(window.location.hostname);
출력값
dev.kfdd6630.net
4. window.location.port = 현재 포트 번호 반환한다.
console.log(window.location.port);
출력값
3000
5. window.location.pathname = 현재 페이지의 경로와 파일 이름 반환한다.
console.log(window.location.pathname);
출력값
/blog/post
6. window.location.search = " ? " 뒤의 파라미터 값 가져온다.
console.log(window.location.search);
출력값
?page=1
7. window.location.protocol = 해당 주소 사용하는 웹 프로토콜 반환한다. (http: or https:)
console.log(window.location.protocol);
출력값
http:
1. window.location을 사용해 URL관련 정보를 가져온다.
2. 가져오고 싶은 URL 정보에 따라 location. 뒤에 필요한 내용을 붙여주면 된다.
반응형
'언어 > Javascript, Typescript' 카테고리의 다른 글
[js] 배열 .filter() 메소드 ( 배열, 객체에서 필터링 ) (0) | 2023.03.30 |
---|---|
AJAX란?, AJAX사용방법 (0) | 2023.03.29 |
[js] 객체안에 함수넣기 (0) | 2023.03.27 |
[js] Dayjs 라이브러리 (설치방법, i18n, 객체생성, get(), set(), DateFormat 설정 등) (0) | 2023.03.24 |
[js] 자바스크립트 Date객체 (기간 조회 및 날짜 계산) (0) | 2023.03.24 |
댓글