예 - URL에서 해시: #section1 샵(#)부터는 해시입니다.
예 - URL에서 해시: #section1 샵(#)부터는 해시입니다.
/* 
 * 현재 URL 예: http://www.example.com/
*/

/*  1. # 기호를 생략하고 문자열만 넣는 경우 */
window.location.hash = "section1"; // URL 결과: http://www.example.com/#section1

// window 객체는 생략할 수 있으며, 보통 location.hash를 사용
location.hash = "section1"; // URL 결과: http://www.example.com/#section1

/*  2. # 기호를 직접 포함해서 문자열을 넣는 경우 */
window.location.hash = "#section1"; // URL 결과: http://www.example.com/#section1

// window 객체는 생략할 수 있으며, 보통 location.hash를 사용
location.hash = "#section1"; // URL 결과: http://www.example.com/#section1
<button type="button" onclick="location.hash='example1'">해시 설정하기</button>
<!-- 또는 -->
<button type="button" onclick="window.location.hash='example1'">해시 설정하기</button>
<button type="button" id="href-btn">해시 설정하기</button>

<script>
    const hrefBtn = document.getElementById("href-btn");
    hrefBtn.addEventListener("click", () => {
        location.hash = "example1";
        // 또는 window.location.hash = "example1";
    });
</script>