location 객체는 현재 사용자가 접속하고 있는 웹 문서의 URL에 대한 정보를 반환하며, 이러한 정보를 가지고 다른 웹 페이지로 이동할 수 있습니다. 형식은 다음과 같습니다.

location.속성

메서드 설 명
reload() 현재 페이지를 새로고침
replace(URL) 현재 페이지에서 다른 URL로 이동.
이전 페이지에 대한 주소 히스토리를 남기지 않음
href 현재 페이지에서 다른 URL로 이동.
다음 중 하나를 이용할 수 있습니다.
  • location.href = "http://example.com";
  • location = "http://example.com";
assign(URL) href 와 동일한 기능
hash 현재 문서의 URL에 붙어있는 해시값
Ex.) http://example.com/?q=w#hash
pathname 현재 문서의 URL 중 도메인을 제외한 파일 경로
host 현재 문서의 호스트 이름과 포트번호
hostname 현재 문서의 호스트 이름
port 현재 문서의 포트번호를 반환
protocol 현재 문서의 프로토콜. http: 나 https: 의 형식
search 현재 문서의 URL 끝에 붙어 있는 쿼리
Ex.) http://example.com/?q=w

현재 문서에서 아래 URL 로 이동합니다.

<script>
  location.replace( "http://example.com" );
</script>

아래 URL이라는 가정에서 추출한 결과입니다.
<script>
/*
  http://example.com:8080/your_name.php?q=w&foo=bar#search
*/


document.write( location.host + "<br/>");
// example.com:8080

document.write( location.hostname + "<br/>");
// example.com

document.write( location.pathname + "<br/>");
// /your_name.php

document.write( location.port + "<br/>");
// 8080

document.write( location.protocol + "<br/>");
// http:

document.write( location.hash + "<br/>");
// #search

document.write( location.search + "<br/>");
// ?q=w&foo=bar
</script>

0 댓글