利用可能なページ番号と現在の URL に基づいて、前の URL と次の URL を設定する実用的なコードがあります。
コードが長すぎて、柔軟性がまったくありません。より柔軟で本番環境に対応するにはどうすればよいですか?
http://jsfiddle.net/sunflowersh/ppDL7/5/
var currentindex = '4'; //updated based on the current index data
var indexNum = parseInt(currentindex);
var currentURL = document.location.href.substring(document.location.href.lastIndexOf("/")+1, document.location.href.length);
var baseURL ="/en_US/";
var leftURL = "";
var rightURL = "";
switch(indexNum)
{
case 2:
// if there are only index, index1, and index2
if((currentURL.indexOf("index.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
case 3:
// if on index3.html or index.html
// make the right arrow URL go back to index1.html
if((currentURL.indexOf("index3.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum-2) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
case 4:
if((currentURL.indexOf("index4.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum-3) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
case 5:
if((currentURL.indexOf("index5.html")>-1)||(currentURL.indexOf("index.")>-1)){
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum-3) + ".html";
} else {
leftURL= baseURL + "index" + parseInt(indexNum-1) + ".html";
rightURL= baseURL + "index" + parseInt(indexNum+1) + ".html";
}
break;
default:
// if no current index, disable the link
leftURL= baseURL + "#";
rightURL= baseURL + "#";
}
var leftArrow =$(".leftArrow").find("a").attr("href",leftURL);
var rightArrow =$(".rightArrow").find("a").attr("href",rightURL);