Javascript分页更新URL

Javascript分页更新URL,javascript,Javascript,我正在尝试使用Javascript分页,但无法将URL更新为?page={pageNumber}。我对100个项目进行了分页,但URL保持不变,我想让页面看起来好像发生了变化。非常感谢您的帮助 我正在尝试实现domain.com/search/到domain.com/search/?page={pageNumber} // Adding Page option select menu if (document.querySelector("#page-no")

我正在尝试使用Javascript分页,但无法将URL更新为?page={pageNumber}。我对100个项目进行了分页,但URL保持不变,我想让页面看起来好像发生了变化。非常感谢您的帮助

我正在尝试实现domain.com/search/domain.com/search/?page={pageNumber}

    // Adding Page option select menu
    if (document.querySelector("#page-no").innerHTML === "") {
        for (let index = 1; index <= totalPages; index++) {
            let option = document.createElement("option");
            option.setAttribute("value", `${index}`);
            option.textContent = index;

            if (index === 1) {
                option.selected = true;
            }

            document.querySelector("#page-no").appendChild(option);
        }
    }

    document.querySelector(".total").textContent =
        apiResult.data.pagination.total_pages;

    // Add event listeners on pagination items
    document.querySelector(".prev").addEventListener("click", prevBtnClick);
    document.querySelector(".next").addEventListener("click", nextBtnClick);
    document.querySelector("#page-no").addEventListener("change", optionSelect);
};

// Event Listener on Previous Button
const prevBtnClick = () => {
    if (currentPage > 1) {
        document.getElementById("page-no").selectedIndex -= 1;
        apiresultFn(animalType, searchLocation, --currentPage);
    }
};

// Event Listener on Next Button
const nextBtnClick = () => {
    if (currentPage < totalPages) {
        document.getElementById("page-no").selectedIndex += 1;
        apiresultFn(animalType, searchLocation, ++currentPage);
    }
};

// Event Listener on Page Menu Change
const optionSelect = (e) => {
    currentPage = parseInt(e.target.value);
    apiresultFn(animalType, searchLocation, currentPage);
};
//添加页面选项选择菜单
if(document.querySelector(“页码”).innerHTML==“”){
对于(设索引=1;索引{
如果(当前页面>1){
document.getElementById(“页码”)。选择的索引-=1;
apiresultFn(animalType,searchLocation,--currentPage);
}
};
//“下一步”按钮上的事件侦听器
const nextBtnClick=()=>{
如果(当前页面<总页面){
document.getElementById(“页码”)。选择的索引+=1;
apiresultFn(animalType,searchLocation,++currentPage);
}
};
//页面菜单更改上的事件侦听器
常量选项选择=(e)=>{
currentPage=parseInt(即target.value);
apiresultFn(动物类型、搜索位置、当前页面);
};

这将添加附加查询字符串。请尝试将其与单击逻辑一起使用

if (history.pushState) {
    var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?page=2';
    window.history.pushState({path:newurl},'',newurl);
}
你的研究关键词是HTML5历史API…