본문 바로가기
언어/Javascript, Typescript

[js] 현재 페이지 URL 관련 정보를 가져오기

by minhyeok.lee 2023. 3. 28.
반응형

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. 뒤에 필요한 내용을 붙여주면 된다.

반응형

댓글