게시물 읽던 중 뒤로가기 버튼은 목록url을 넣어야 할까? 히스토리백을 넣어야할까?

<a href="{getUrl('document_srl','','comment_srl','','member_srl',$member_srl)}" data="history.back(); return false;" class="button btn-primary"><i class="back"></i></a>

게시물 읽기 중 뒤로가기는 history.back이 맞는거 같은데. 음

왜 자꾸 헷갈린 건까요?


참조

https://xetown.com/questions/1407780

https://dirtycoders.net/jabaseukeuribteu-dwirogagi-ibenteu-gamjihagi-yejewa-eungyong/


// >>>>> goback();
// 게시물 상세 보기로 이동할 때 현재 페이지 URL 저장
if (document.referrer && document.referrer.includes(window.location.origin)) {
    sessionStorage.setItem("prevPage", document.referrer);
    console.log(sessionStorage.getItem("prevPage"));
}
function goBack() {
    const prevPage = sessionStorage.getItem("prevPage");
    if (prevPage) {
       location.href = prevPage;
    } else {
       location.href = "/board/list";
    }
}

<a href="javascript:;" onclick="goBack(); return false;" class="button btn-primary"><i class="back"></i></a>

아 문제를 찾았네요. 게시물 수정하기가 이전화면일 때가 문제였던거였네요 참.

조건1. 이전 화면이 게시물 수정 화면이었을 경우, 리스트로 이동

조건2. 이전 화면이 다른 게시물 읽기 화면이었을 경우, 리스트로 이동(방향키로 이전 게시물 읽기 하고 있을 때 목록으로 빠져나가는 기능이 없음)

조건3. 이전 화면이 외부사이트였을 경우, 리스트로 이동함.

조건4. 백스페이스키 입력시 이전화면으로 이동함.

- 댓글 쓰기일 때 동작 막음

- 인풋 등일 때 동작 막음

- 에디터에 따라서 추가 수정이 필요할 수 있음.

조건5. 게시판id/write일 경우 게시판id로 이동


// 게시물 상세 보기로 이동할 때 현재 페이지 URL 저장
if (
    document.referrer &&
    document.referrer.includes(window.location.origin) &&
    document.referrer !== window.location.href // 현재 페이지와 다를 때만 저장
) {
    sessionStorage.setItem("prevPage", document.referrer);
}

// 뒤로 가기 버튼 기능
function goBack() {
    const prevPage = sessionStorage.getItem("prevPage");

    if (!prevPage) {
       // 이전 페이지 정보가 없거나, 외부 사이트에서 온 경우 홈으로 이동
       location.href = window.location.origin;
       return;
    }

    // 현재 사이트 기준으로 경로 부분만 가져오기
    const urlPath = new URL(prevPage).pathname; // ex: /promotion/470926, /board/472332/edit, /threads/write

    // /숫자/edit 또는 /숫자 → /게시판ID/ 로 변경
    let modifiedPath = urlPath.replace(/\/\d+(\/edit)?$/, "/");

    // /게시판ID/write → /게시판ID/ 로 변경
    modifiedPath = modifiedPath.replace(/\/write$/, "/");

    location.href = window.location.origin + modifiedPath;
}

// Backspace 키 이벤트 추가 (입력 필드 제외)
document.addEventListener("keydown", function (event) {
    // Backspace 키 감지
    if (event.key === "Backspace") {
       // 현재 포커스된 요소 가져오기
       const activeElement = document.activeElement;

       // 입력 필드, contenteditable 요소, form 내부, 댓글 입력창 활성화 여부 확인
       const isInputFocused = ["TEXTAREA", "INPUT", "SELECT"].includes(activeElement.tagName);
       const isContentEditableFocused = activeElement.isContentEditable;
       const isInsideForm = activeElement.closest("form");
       const isCommentPostWrapActive = document.querySelector(".comment-post-wrap.active");

       // 입력 관련 요소에 포커스가 있거나 댓글 입력창이 활성화된 경우 Backspace 작동 유지
       if (isInputFocused || isContentEditableFocused || isInsideForm || isCommentPostWrapActive) {
          return;
       }

       // 기본 Backspace 동작 막기
       event.preventDefault();
       goBack(); // 뒤로 가기 실행
    }
});


3