JS/JS 공부

[JS] location 객체 사용 (url로 이동, 페이지 새로고침)

congs 2023. 4. 21. 12:48

location 객체

: 브라우저의 주소표시줄 입력에 관련된 객체

 

location.href = 'url' ;     : 해당 url로 이동

  • 히스토리 객체에 이동 정보를 저장함
  • = 뒤로가기 가능
 
 <button id="href">href 사용 이동 (네이버) </button>
 
<script>
 
document.getElementById('href').addEventListener('click',()=>{
            location.href='https://www.naver.com/';
        })
 
</script>
 

 

 

location.replace('url');      : 해당 url로 이동

  • 히스토리 객체에 이동 정보를 저장하지 않음
  • = 뒤로가기 불가능
 
<button id="replace">replace 사용 이동 (다음) </button>
 
<script>

        document.getElementById('replace').addEventListener('click',function(){
            location.replace('https://www.daum.net/');
        })
    

</script>
 

 

 

location.assign('url');       : 해당  url로 이동

  • 히스토리 객체에 이동 정보를 저장함
  • = 뒤로가기 가능
 
<button id="assign">assign 사용 이동 (유튜브) </button>
 
<script>
 
     
      //함수 + onclick 방법
       function assign(){
            location.assign('https://www.youtube.com/');
       }
       document.getElementById('assign').addEventListener('click',assign);

</script>
 

 

 

location.relode();      : 현재 페이지 새로고침

  • onclick = ' window.location.reload() ' 로 바로 사용도 가능!
 
<button onclick="window.location.reload()">reload 사용 새로고침</button>