location

치로·2024년 8월 23일
  • 웹 브라우저으이 주소 표시줄을 제어
  • 그 일부를 조회할 수 있으며, 변경할 수도 있음

1. location 정보

  • location.href : 문서의 URL 주소
  • location.host : 호스트 이름과 포트
  • location.pathname : 디렉토리 이하 경로
  • location.port : 포트 번호
  • location.protocol : 프로토콜 종류
  • location.search : 조회 부분
  • location.href = "주소값" : 페이지 이동
  • location.reload() : 페이지 새로고침
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="button" value="네이버로 이동하기" onclick="goNaver()">
    <script>
        //function goNaver(){
       //     if(confirm("네이버로 이동하시겠습니까?")) {
                location.href = "https://www.naver.com";
        //    }
      //  }
      if(confirm("새로 고침 하시겠습니까?")) {
                location.href = location.reload();
            }
    </script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body onload="authNo()">
    <p>
        고객님의 인증번호는 
        <strong id="auth">00000</strong>
        입니다
    </p>
    <input type="button" value="인증번호 새로 받기"
        onclick="refresh()"/>
    <script>
        function refresh(){
            location.reload();
        }
        function authNo(){
            // 5자리 인증번호를 id값이 "auth"인 요소에게 출력
            // console.log("authNo 실행!!!!");
            let value = "";
            for( let i=0; i<5; i++ ){
                value += random(0,9);
            }
            document.getElementById("auth").innerHTML = value;
        }
        function random(n1, n2){
            return parseInt(Math.random() * (n2-n1+1)) + n1;
        }
    </script>
</body>
</html>

0개의 댓글