본문 바로가기

프로그래밍/자바스크립트

[자바스크립트]클라이언트 객체 : location, history

1. location 객체의 속성과 메서드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>클라이언트 객체 : location</title>
<script type="text/javascript">
    document.write('location.href : ' + location.href +'<br>');                //주소
    document.write('location.host : ' + location.host + '<br>');            //호스트명, 포트번호
    document.write('location.hostname : ' + location.hostname + '<br>');    //호스트명
    document.write('location.port : ' + location.port + '<br>');            //포트번호
    document.write('location.protocol : ' + location.protocol + '<br>');    //http:
    document.write('location.pathname : ' + location.pathname + '<br>');    //경로정보
</script>
//출력결과
location.href : http://localhost:11742/HTML/study/ch01.html
location.host : localhost:11742
location.hostname : localhost
location.port : 11742
location.protocol : http:
location.pathname : /HTML/study/ch01.html
</head>
<body>
</body>
</html>
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>클라이언트 객체 : location</title>
</head>
<body>
<!-- 페이지 이동 할 수 있고, history정보가 남아있어서 뒤로가기 가능(원래 페이지로 돌아갈 수 있음) -->
<input type="button" onclick="location.href='s02_window.html'" value="이동(href)">        <!-- 속성 이용 -->
<input type="button" onclick="location.assign('s02_window.html')" value="이동(assign)">    <!-- 메소드 이용 -->
 
<!-- 페이지 이동 할 수 있고, history정보가 지워져서 뒤로가기 불가 -->
<input type="button" onclick="location.replace('s02_window.html')" value="이동(replace)"> 
 
<!-- 새로고침 -->
<input type="button" onclick="location.reload()" value="새로고침">
 
</body>
</html>
cs

 

2. history

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>history</title>
</head>
<body>
<input type="button" value="이전 페이지로 이동(back)" onclick="history.back();">
<input type="button" value="이전 페이지로 이동(go)" onclick="history.go(-1);">    <!-- 음수 : 이전페이지, 양수 : 다음페이지 -->
<input type="button" value="앞 페이지로 이동(forward)" onclick="history.forward();">
<input type="button" value="앞 페이지로 이동(go)" onclick="history.go(1);">
</body>
</html>
cs