所以我建立了这个日记网站作为自己的消遣。我将所有条目作为单独的网页放在一个文件夹中。我注意到的一件事是我无法持续阅读该网站。
我想添加根据当前网址进行重定向的按钮。前任。在第 5.html 页上,下一个按钮将重定向到 6.html,上一个按钮将重定向到 4.html。
我的所有条目都遵循上述命名方案,所以这不是问题。
我能想到的唯一解决方案是手动添加具有正确链接的按钮。有 35 页(更多),所以我不急于暴力破解它
我绝对认为有更好的方法可以做到这一点,所以我问了一些比我更聪明的人。
JS代码如下
let str = window.location.href;
console.log("Original String: " + str + "<br/>");
// Matching for the numbers using the match() method.
let numbers = str.match(/\d+/g);
console.log("Numbers: " + numbers + "<br/>");
// Converting the numbers into integer using parseInt() method.
numbers = parseInt(numbers, 10);
let nexturl = "file:///home/nakul/gitclones/thegoodjar/entries/" + (numbers+1) + ".html";
function next (){
location.assign(nexturl);
}
function prev (){
location.assign("file:///home/nakul/gitclones/thegoodjar/entries/" + (numbers-1) + ".html");
}
还有很大的改进空间,但主要是我应该致力于使代码可移植。