코딩하는 문과생

자바스크립트/ 09. location 객체와 history 객체 본문

웹 프로그래밍/Javascript

자바스크립트/ 09. location 객체와 history 객체

코딩하는 문과생 2018. 11. 4. 14:53

09. location 객체와 history 객체


-location 객체


href: 현재페이지의 href(URL)반환

hostname: 웹호스트의 도메인 이름을 반환

pathname: 현재 페이지의 경로와 파일 이름을 반환

protocol: 사용된 웹 프로토콜을 반환


1
2
3
4
5
6
7
8
9
10
11
<body>
  <p id="demo"></p>
</body>
<script>
  document.getElementById("demo").innerHTML =
  "page location is: " + window.location.href + "<br>" +
  "page hostname is: " + window.location.hostname + "<br>" +
  "page path is: " + window.location.pathname + "<br>" +
  "page protocol is: " + window.location.protocol + "<br>"
</script>
 
cs








-history 객체

페이지 뒤로가지 앞으로 가기와 동일


1
2
3
4
5
6
7
8
9
10
11
12
13
<body>
  <input type="button" onclick="goBack()" value="Back">
  <input type="button" onclick="goForward()" value="Forward">
</body>
<script>
  function goBack(){
    window.history.back()
  }
  function goForward(){
    window.history.forward()
  }
</script>
 
cs


<실행결과>