[Java Script] location

yihyun·2024년 8월 26일

Front-end

목록 보기
12/22

location

로케이션은 위치 정보를 얻을 때 사용한다.

  • console.log(location.href); 현재 페이지가 존재하는 서버상의 전체 경로
  • console.log(location.protocol); 웹 프로토콜(http, https, ws, ...) 을 알려준다.
  • console.log(location.hostname); 도메인 또는 ip 주소를 의미
  • console.log(location.pathname); hostname을 제외한 나머지 주소
  • console.log(location.port); port 번호

※ console.log 의 경우 개발자 모드에서 확인하기 위해 사용함 (없어도 문제x)

location.href 는 a 태그의 href 처럼 페이지 이동을 시켜주기 때문에 주로 페이지를 이동할 때에 사용된다.

<body>
  <button onclick="move()">move</button>
</body>

<script>
    function move(){
    	location.href = 'https://www.naver.com'; // 버튼을 클릭하면 네이버로 이동한다. 
    }
</script>
profile
개발자가 되어보자

0개의 댓글