I'd appreciate any help just refining this working code. It has a bit of a weird context, but basically it's for a gallery style site that has hundreds of pages, but needs new navigation buttons. Unfortunately there's no way to add these buttons without adding them for over 300+ pages manually, or using a javascript like this.
Let's say we have a URL like the following (this coincides with the site's URL structure):
http:/test.com/10-funny-pictures/
The script links next button that links to http:/test.com/10-funny-pictures/2/, and hides the last button.
If the last page in the gallery is http:/test.com/10-funny-pictures/10/, then it sets the last button to link to http:/test.com/10-funny-pictures/9/. But on the last page of a gallery, there is a javascript inserted on that last page that holds the URL of a new gallery to send the user to when the click next, that URL is passed to the main JavaScript and then the next button links to that rather than http:/test.com/10-funny-pictures/11/ (which doesn't exist).
It's messy, but the JSFiddle is right here, any help making the code more efficient would be appreciated. I just kind of made it but I'm not good at doing things the best way when I code.
Last<div id="rightcontainer"><a href="#" id="nextlink"><div id="nextbutton">Next</div></a></div>
<script type='text/javascript' onload="linkSet()">
function linkSet() {
var currentURL = location.href;
var lastURL = "";
var nextURL = "";
var lastChar = currentURL.charAt(currentURL.length-2);
var secondLastChar = currentURL.charAt(currentURL.length-3);
var oneRoot = currentURL.slice(0, -2);
var twoRoot = currentURL.slice(0, -3);
if(isNaN(lastChar) == true) {
var leftContain = document.getElementById('leftcontainer');
var rightContain = document.getElementById('rightcontainer');
function styleSet() {
leftContain.style.display = "none";
rightContain.style.width = "100%";
rightContain.style.float = "none";
};
styleSet();
lastURL = currentURL;
nextURL = currentURL + "2/";
} else if(isNaN(secondLastChar) == false) {
var twoslash = currentURL.substr(currentURL.length-3);
var twodigit = twoslash.substr(0,2)*1;
if(twodigit == 10) {
lastURL = twoRoot + "9/";
nextURL = twoRoot + "11/";
} else {
lastURL = twoRoot + (twodigit - 1) + "/";
nextURL = twoRoot + (twodigit + 1) + "/";
}
} else {
var oneslash = currentURL.substr(currentURL.length-2);
onedigit = oneslash.substr(0,1)*1;
if(onedigit == 2) {
lastURL = oneRoot;
nextURL = oneRoot + "3/";
} else {
lastURL = oneRoot + (onedigit - 1) + "/";
nextURL = oneRoot + (onedigit + 1) + "/";
}
}
if (typeof urlData !== 'undefined') {
nextURL = urlData;
}
document.getElementById("lastlink").href = lastURL;
document.getElementById("nextlink").href = nextURL;
}
window.onload = linkSet;
</script>
<!---
EXAMPLE OF THE FINAL GALLERY PAGE SCRIPT THAT PASSES URL VALUE ON A GALLERY'S LAST PAGE!
<script type='text/javascript'>
var urlData = "http://www.test.com/10-crazy-things/";
</script>
-->
#lastbutton {
display: inline-block;
width: 80px;
height: 20px;
background-color: #0a72c2;
color: #ffffff;
text-decoration: none;
text-align: center;
padding: 8px;
font-size: 16px;
}
#nextbutton {
display: inline-block;
width: 80px;
height: 20px;
background-color: #0a72c2;
color: #ffffff;
text-decoration: none;
text-align: center;
padding: 8px;
font-size: 16px;
}
#leftcontainer {
margin-top: 30px;
float: left;
text-align: center;
width: 50%;
}
#rightcontainer {
margin-top: 30px;
text-align: center;
float: right;
width: 50%;
}