Solved

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

Posted on 2013-05-09
2
318 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

808 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