I'm trying to create a previous button that will cycle through an array of urls. I got it to work as intended, however I had to use .indexOf() when checking if the current url is greater than the first item in the array. Since the urls will most likely be changing, I don't want to specify the exact url.
Here is the working example:
var pageList = ["january_28-2016.html", "february_06-2016.html", "march_17-2016.html"]
var url = window.location.pathname;
var page = url.substring(url.lastIndexOf('/')+1);
var currentPosition = pageList.indexOf(page);
document.getElementById('prev-btn').onclick = function(){
if(currentPosition>pageList.indexOf("january_28-2016.html")) {window.location = pageList[--currentPosition];}
else {window.location = pageList[pageList.length-1];}
}
I tried
if(currentPosition>pageList[0])
and
var page1 = pageList[0]
if(currentPosition>page1)
but could not get it to function. Is there a better way of accomplishing this without using pageList.indexOf("january_28-2016.html")) to get the first element?