본문 바로가기

Dev. Language/Javascript

location 객체


location 객체의 프로퍼티는 전부 페이지의 로케이션과 관련되어 있다.
[표]

 프로퍼티 용도
 hash  http://some.com/somepage#somehash
(해시마크(#) 뒤에 오는 문자열)
 host  URL의 호스트명 + 포트번호
 hostname  호스트명(도메인명)
 href  URL 전체
 pathname  도메인명 다음에 오는 경로명
 port  포트 번호
 protocol  프로토콜(HTTP 등)
 search  질의 문자열(?) 뒤에 오는 문자열
 target  URL target 명

예)
http://learningjvascript.info/ch09-01.htm?a=1

host/hostname: learningjavascript.info
protocol: http
search: a=1
href: http://learningjavascript.info/ch09-01.htm?a=1


[예제] 두 프레임을 로드하는 예 - location 객체 이용.
main.html
<!-- body -->
<frameset cols="300, *">
<frame name="frameone" src="frame1.html"/>
<frame name="frametwo" src="frame2.html"/>
</frameset>

frame1.html
<!-- body -->
<h3>Helpful Information.</h3>
<h1>Frame 1</h1>

frame2.html
<h3>Frame 2</h3>
<a href="noway.html">Open Noway</a>

noway.html
<!-- script -->
if(self != top) {
   if(window.location.href.replace)
      top.location.replace(self.location.href);
   else
      top.location.href=self.document.href;
}

<!-- body -->
<h1>이 웹페이지를 프레임 안에서 열지 못하도록 했습니다.</h1>

* 결과 : noway.html 링크를 클릭하면 프레임의 안이 아니라 프레임을 가진 메인에서 로딩된다.