• 회원가입
  • 로그인
  • 구글아이디로 로그인

[BOM] JS - Window Location 객체 - 위치객체 (= 로케이션객체 = Location객체) ※ location.href 로케이션허프 ※ 주소이동/납치태그

목차

  1. window.location 객체 정의
  2. window.location.href 속성 - URL 반환
  3. window.location.hostname 속성 - 호스트명 반환
  4. window.location.pathname 속성 - 경로 반환
  5. window.location.protocol 속성 - 프로토콜 반환
  6. window.location.port 속성 - 포트 반환
  7. window.location.assign() 메서드 - URL 이동

 

window.location 객체 정의

 

현재 웹페이지 주소(URL) 얻거나 새 웹페이지로 브라우저 리디렉션.

 


[window.location 객체의 속성/메서드]

※ window 접두어 없이 사용 가능.

 

  • window.location.href 속성: 현재 페이지의 href (URL) 반환
  • window.location.hostname 속성: 웹 호스트명(=도메인명) 반환
  • window.location.pathname 속성: 현재 페이지 경로와 파일명 반환
  • window.location.protocol 속성: 웹 프로토콜 (http:/https:) 반환
  • window.location.assign() 메서드: 현재창에서 새 URL 주소로 이동

 
PS. 부모창에 띄우려면, window.top.location.href 이용.


window.location.href 속성 - URL 반환

[현재 URL 반환]

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML =  window.location.href;

</script>

 

결과보기


[이동할 URL 설정]

 

<input type="button" value="클릭" onclick="homzzang()">


<script>

function homzzang() {

  window.location = "https://homzzang.com";

}

</script>

 

결과보기


window.location.hostname 속성 - 호스트명 반환

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML =  window.location.hostname;

</script>

 

결과보기

예제: http://cdpn.io/

홈짱: homzzang.com

 

window.location.pathname 속성 - 경로 반환

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML =  window.location.pathname;

</script>

 

결과보기

예제: /boomboom/v2/index.html

홈짱: /test.php

 

window.location.protocol 속성 - 프로토콜 반환

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML =  window.location.protocol;

</script>

 

결과보기

예제: https:

홈짱: https:

 

window.location.port 속성 - 포트 반환

 

<p id="demo"></p>


<script>

document.getElementById("demo").innerHTML =  window.location.port;

</script>

 

결과보기

※ 대부분 브라우저는 기본 포트 번호 경우, 0 표시하거나 아무 것도 표시 X

※ 기본 포트 번호: (http 경우 80 / https 경우 443)

※ 홈짱: 아무 것도 표시 안 됨.

 

window.location.assign() 메서드 - URL 이동

 

<input type="button" value="클릭" onclick="homzzang()">


<script>

function homzzang() {

  window.location.assign("https://homzzang.com");

}

</script>

 

결과보기


PS. location.assign() 메서드 특징

 

  1. 현재창에서 해당 URL 주소로 이동
  2. 뒤로가기 가능
  3. 마우스허버 시 URL 주소 노출 X 

 

※ 해당 웹페이지가 iframe 안에 안 들어도록 설정된 경우 로딩 안 될 수 있음.



분류 제목
String JS - concat() 메서드 ★ - 문자열결합 (= 문자열합치기 = concat메서드 = 컨캣메서드)
String JS - endsWith() 메서드 - 지정문자열로 끝나는지(=종료) 여부 (IE12이상)
String JS - fromCharCode() 메서드 - 유니코드를 문자열로 변환
String JS - includes() 메서드 ★ - 지정문자열포함여부 (= includes메서드 = 인클루드즈메서드)
String JS - indexOf() 메서드(문자열) ★ - 처음일치문자열위치찾기 (= 문자열포함검사 = 인덱스어브메서…
String JS - lastIndexOf() 메서드(문자열) ★ - 마지막일치문자열위치찾기 (= 문자열포함검사 = 라스…
String JS - localeCompare() 메서드 - 문자열순서비교
String JS - match() 메서드 ★ - 일치하는 문자열 반환 (= match메서드 = 매치메서드) ※ 정규식 …
String JS - repeat() 메서드 - 지정횟수만큼 문자열반복 (= 리피트)
String JS - replace() 메서드(String용) ★ - 문자열 대체 (= replace메서드 = 리플레이스…
String JS - search() 메서드 - 문자열위치찾기 ※ 문자열포함검사
String JS - slice() 메서드 - 문자열 자르기 (= 문자열 일부 추출 = slice메서드 = 슬라이스 메서…
String JS - split() 메서드 - 문자열 쪼개기 (= split메서드 = 스플릿 메서드) ※ 이메일숨기기 (…
String JS - startsWith() 메서드 - 지정문자열로 시작 여부 (IE12이상)
String JS - substr() 메서드 ★ - 문자열 자르기 - 문자열의 특정 위치 이후의 특정 길이 만큼 반환
String JS - substring() 메서드 ★ - 문자열 일부 추출 (= 문자열 자르기 = substring메서드…
String JS - toLocaleLowerCase() 메서드 - 로캘 소문자로 변환
String JS - toLocaleUpperCase() 메서드 - 로캘 대문자로 변환
String JS - toLowerCase() 메서드 - 소문자로변환
String JS - toString() 메서드 (문자열경우) - 문자열타입으로 변경 (= toString메서드 = 투스…
6/67
목록
찾아주셔서 감사합니다. Since 2012