Location 객체

Location

문서의 주소와 관련된 객체로 Window 객체의 프로퍼티

  1. 브라우저의 창에 열려있는 문서의 URL을 알려주며 변경도 가능

  2. 문서의 위치와 관련해서 다양한 정보를 얻을 수 있음

1. 현재 윈도우의 URL 알아내기

console.log(location.toString(), location.href);

location.href 방식을 더 선호 함

2. URL Parsing

location 객체는 URL을 의미에 따라서 별도의 프로퍼티로 제공함

console.log(location.protocol, location.host, location.port, location.pathname, location.search, location.hash)

location(URL) = http://example.com:8080/test.jsp?gg=1&pp=2#q=html5

  1. location.protocol : ex) http:

  2. location.host : 컴퓨터를 식별하는 주소 ex) example.com:8080

  3. location.port : 그 컴퓨터에서 돌아가는 여러가 소프트웨어들을 식별하는 번호 ex) 8080

  4. location.pathname : 웹 서버에 접속했을 때 구체적인 정보를 요청하는 정보 ex) test.jsp

  5. location.search : 애플리케이션에 전달한 값을 알고싶을 때 ex) ?gg=1&pp=2

  6. location.hash : 특정한 위치를 식별하는 식별자이자 일종의 북마크 ex) #q=html5

3. URL 변경하기

3-1. 사용자가 다른 URL로 이동시켜야 할 때

location.href = 'http://egoing.net';
//location = 'http://egoing.net';

3-2. 현재 웹페이지 내용을 리로드하고 싶을 때

location.reload();

Last updated