Solved

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

Posted on 2013-05-09
2
320 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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 …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

691 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