Solved

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

Posted on 2013-05-09
2
313 Views
Last Modified: 2013-05-10
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

0
Comment
Question by:sunflowersh
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39155479
What is the problem with the following?
var indexNum = currentindex * 1;
var prevIndexNum = indexNum - 1;
var nextIndexNum = indexNum + 1;
 
baseURL += "index";
var leftArrow = $(".leftArrow").find("a").attr("href", baseURL + prevIndexNum + ".html");
var rightArrow =$(".rightArrow").find("a").attr("href", baseURL + nextIndexNum + ".html");

Open in new window

0
 
LVL 2

Author Closing Comment

by:sunflowersh
ID: 39157190
Thank you!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now