본문으로 바로가기

 

* URL 변경 후 새로고침 하면  

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    document.addEventListener('DOMContentLoaded', function () {
        // "b" 파라미터 변경 버튼
        document.getElementById('changeParamBtn').addEventListener('click', function () {
            updateURLParam('b', 1);
        });

        // URL 파일명 변경 버튼
        document.getElementById('changeFileBtn').addEventListener('click', function () {
            updateURLFile('abc.html');
        });
    });

    // "b" 파라미터 변경 함수
    function updateURLParam(param, value) {
        let url = new URL(window.location.href);
        let params = url.searchParams;

        if (params.has(param)) {
            let currentValue = parseInt(params.get(param), 10);
            params.set(param, currentValue + 1);
        } else {
            params.set(param, value);
        }

        let newUrl = url.pathname + '?' + params.toString();
        window.history.pushState({}, '', newUrl);
    }

    // 파일명을 변경하는 함수
    function updateURLFile(newFileName) {
        let url = new URL(window.location.href);
        let pathParts = url.pathname.split('/');

        // 마지막 요소(파일명)를 새로운 파일명으로 변경
        pathParts[pathParts.length - 1] = newFileName;

        let newPath = pathParts.join('/');
        let newUrl = newPath + url.search; // 기존 쿼리 파라미터 유지

        window.history.pushState({}, '', newUrl);
    }
</script>

<button id="changeParamBtn">b 값 변경</button>
<button id="changeFileBtn">파일명을 abc.html로 변경</button>

</body>
</html>

 

 

다른 형식도 충분히 가능 함