javascript - prev / next nav codes to be more efficient and flexible -
i have working codes sets prev , next url based on available pages numbers , url on.
my codes way long , not flexible @ all. how can make more flexible , production ready?
http://jsfiddle.net/sunflowersh/ppdl7/5/
var currentindex = '4'; //updated based on 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 index, index1, , 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 right arrow url go 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 link lefturl= baseurl + "#"; righturl= baseurl + "#"; } var leftarrow =$(".leftarrow").find("a").attr("href",lefturl); var rightarrow =$(".rightarrow").find("a").attr("href",righturl);
have see @ http://jsfiddle.net/steelywing/ppdl7/6/
create index.js
var baseurl = "/en_us/index"; var maxindex = 4; function updatenav(index) { if (index-1 > 0) { $('.leftarrow a').attr('href', baseurl + (index-1) + '.html'); } else { $('.leftarrow a').hide(); } if (index+1 <= maxindex) { $('.rightarrow a').attr('href', baseurl + (index+1) + '.html'); } else { $('.rightarrow a').hide(); } } and, in every index.html, call updatenav() onload
<script>updatenav(2); // 2 current index </script> if need parse url, try using regexp /\/index(\d+)\.html$/ (http://jsfiddle.net/steelywing/uex5m/)
Comments
Post a Comment