If there is a prev button next button, can I change the URL by making it i-- when I press the prev button, and i++ when I press the next button?

Asked 1 years ago, Updated 1 years ago, 45 views

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()
})

The total file consists of index.html with this code and page files to be replaced: 1.html, 2.html, and 3.html. When the prev button is pressed, -> i changes the address by adding the value of i and the string ".html" to the index value of the current address +1. When the next button is pressed, -> i value becomes +1 and the rest is the same.
When the next button is pressed at first, it can be moved to 1.html, but it can't be moved to 2.html anymore.

How can we solve this problem? Please point it out!

url javascript

2022-09-22 15:06

1 Answers

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.