반응형

자바스크립트 개발 환경 Microsoft Visual Studio 2015 버전 HTML 기능 및 Chrome을 이용하고 있습니다.





이번에는 window.location의 몇가지 예제에 대해 알아보려한다.


window.location.href :: 현재 페이지 URL을 리턴해준다.

window.location.hostname :: 웹 호스트의 도메인 이름을 리턴해준다.

window.location.pathname :: 현재 페이지의 파일이름, 경로를 리턴해준다.

window.location.protocol :: 웹 프로토콜을 알려준다.(http인지 https인지)

window.location.assign :: 새로운 document를 불러준다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html>
<body>
 
    <h1>JavaScript With Crocus</h1>
 
    <p>아래 버튼을 누르면 현재 페이지 정보가 나타납니다.</p>
    <button type="button" onclick="getInfo()"> << 클릭 >> </button>
    <p id="test"></p>
 
 
    <p>아래 버튼을 누르면 새로운 document가 나타납니다.</p>
    <button type="button" onclick="getNew()"> << 클릭 >> </button>
    <p id="test2"></p>
 
    <script>
        function getInfo() {
            var href = window.location.href;
            var hostname = window.location.hostname;
            var pathname = window.location.pathname;
            var protocol = window.location.protocol;
            
            document.getElementById("test").innerHTML =
                "href :: " + href + "<br>" +
                "hostname :: " + hostname + "<br>" +
                "pathname :: " + pathname + "<br>" +
                "protocol :: " + protocol + "<br>";
        }
 
        function getNew(){
            window.location.assign("http://www.crocus.co.kr");
        }
     </script>
 
</body>
</html>
 
//                                                       This source code Copyright belongs to Crocus
//                                                        If you want to see more? click here >>
Crocus



클릭하시면 결과 화면을 확인 할 수 있습니다.


















반응형

'Basic > JavaScript' 카테고리의 다른 글

자바스크립트 쿠키(Cookie) 개념 및 사용 방법  (1) 2017.09.28
자바스크립트 navigator  (0) 2017.09.28
자바스크립트 screen  (0) 2017.09.28
자바스크립트 window  (0) 2017.09.28
자바스크립트 input 그리고 submit  (0) 2017.09.26