혹시 prev버튼 next버튼있다고 하면 prev버튼을 눌렀을떄 i--, next버튼을 눌렀을떄 i++ 가 되도록하여 URL을 변경할 수 있나요?
조회수 850회
var prevBtn = document.getElementById("prev");
var nextBtn = document.getElementById("next");
var i = 0;
var getPathname = window.location.pathname;
var repetitivePath = getPathname.substring(0, getPathname.lastIndexOf("/") + 1);
function minus(){
i--;
if ( i === -1){
i = 0;
}
// var repetitivePath = getPathname.substring(0, getPathname.lastIndexOf("/") + 1);
// window.location.href = repetitivePath + i + ".html";
window.location.replace(repetitivePath + i + '.html');
console.log(i);
// console.log(repetitivePath);
}
function plus(){
i++;
if ( i === 3){
i = 2;
}
// var repetitivePath = getPathname.substring(0, getPathname.lastIndexOf("/") + 1);
// window.location.href = repetitivePath + i + ".html";
window.location.replace(repetitivePath + i + '.html');
console.log(i);
// console.log(repetitivePath);
}
prevBtn.addEventListener("click", function(){
minus();
})
nextBtn.addEventListener("click", function(){
plus()
})
총 파일은 이 코드가 있는 index.html과 바뀔 페이지 파일들인 1.html, 2.html, 3.html로 구성.
prev버튼을 눌렀을때 -> i값이 -1되면서 현재 주소의 인덱스값+1 로 하여 i의 값과 ".html" 문자열을 합하여 주소를 변경.
next버튼을 눌렀을때 -> i값이 +1되면서 나머지 동일.
처음에 next버튼을 눌렸을때 1.html로는 이동이 됩니다만 2.html로는 더이상 진행이 안됩니다.
어떤 식으로 접근하면 이 문제를 해결 할 수 있을까요? 선배님의 지적 부탁드립니다!
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
1.html로 이동하게되면, 페이지가 새로 로딩이 될테니깐, 변수 i 값은 다시 0이 됩니다.
http://domain.com/1.html 이라면
/1.html, /2.html, /3.html의 숫자 값을 가져와서 페이지를 제어하는 방법도 있겠네요.
댓글 입력