javascript

Location, history.back, history.forward 함수

저스크라이크헤븐 2019. 11. 27. 21:03

 

Location : 현재 있는 주소를 알려한다.

 

 

<body>

    <p id="demo"></p>

    <button id="loc">window.html 페이지 이동</button>

    <input type="button" value="뒤로" onclick="goBack()">

    <input type="button" value="앞으로" onclick="goForward()">

 

</body>

<script>

    document.getElementById("demo").innerHTML="page location is: "+location.href; //현재 위치한 주소값을 알려준다.

    document.getElementById("loc").addEventListener("click",function(){

        window.location.href="http://www.naver.com"; //이런식으로 주소값을 주면 그 장소로 이동할 수 있다.

    });

 

    function goBack(){

        //window.history.back(); //뒤쪽으로 이동 

        window.history.go(-1); //-1은 전 페이지로 -2는 전전 페이지로 이동

    }

    

    function goForward(){

        window.history.forward(); //앞쪽으로 이동

    }