Java Script & DOM/Java Script

URL 정보 객체: Location 객체

본클라쓰 2010. 12. 16. 12:05

 

location 객체는 자바스크립트 객체로 현재 URL정보를 가지고 있다.

 

 

location 객체의 프로퍼티

 

프로퍼티

설명

host

호스트명과 포트 번호를 반환한다.

hostname

호스트명을 반환한다.

href

전체 URL 정보를 반환한다.

pathname

path 이름을 반환한다.

port

포트 번호를 반환한다.

protocol

프로토콜을 반환한다.

search

쿼리(Query) 정보를 반환한다.

 

 

location 객체 메소드

 

메소드

설명

assign()

새로운 문서를 로드한다.

reload()

현재 문서를 다시 로드한다.

replace()

현재 문서를 새로운 문서로 대체한다.

 

 

사용 예제)

document.write("host: "+location.host+"<br />");
document.write("hostname: "+location.hostname+"<br />");
document.write("href: "+location.href+"<br />");
document.write("pathname: "+location.pathname+"<br />");
document.write("port: "+location.port+"<br />");
document.write("protocol: "+location.protocol+"<br />");
document.write("search: "+location.search+"<br />");

 

결과)

host: www.w3schools.com
hostname: www.w3schools.com
href: http://www.w3schools.com/jsref/tryit_view.asp
pathname: /jsref/tryit_view.asp
port:
protocol: http:
search: