Link to home
Start Free TrialLog in
Avatar of sunflowersh
sunflowersh

asked on

jQuery, JavaScript - help with optimizing Prev / Next navigation codes

I have working codes that get a variable that tells the number of available index pages. Based on the number and the current file name, sets a Prev page and Next page URL.

Current codes are too long and not very flexible. How can I make my codes more optimized and be production ready?

Here is JSFiddle

e.g)
If index3.html is not available, index2.html's next page URL is index1.html
index.html (index.html is a landing page only and doesn't need to be included in the prev nor next page navigation)
index1.html
index2.html
index3.html..

HTML

<div class="leftArrow"><a href="#"><<</a></div>
<div class="rightArrow"><a href="#">>></a></div>

Open in new window

JavaScript

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);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sunflowersh
sunflowersh

ASKER

Thank you!