views:

46

answers:

2

Here is my code so far:

var linkElement = document.getElementById("BackButton");
var loc_array = document.location.href.split('/');
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2]))); 
linkElement.appendChild(newT);

Currently it takes the second to last item in the array from the URL. However I want to do a check for the last item in the array to be "index.html" and if so grab the third to last instead.

+3  A: 
if(loc_array[loc_array.length-1] == 'index.html'){
 //do something
}else{
 //something else.
}

In the event that your server serves the same file for "index.html" and "inDEX.htML" you can also use: .toLowerCase().

Though, you might want to consider doing this server-side if possible. it will be cleaner and work for people without JS.

Aaron Harun
Sadly I am restricted from doing so where I am. I'm an C# guy.
Bry4n
A: 

Will this work?

if (loc_array.pop() == "index.html"){
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-3])));
}
else{
var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length-2])));
}
Bry4n
No because `.pop()` returns the last element and also removes it, so your indexes change.
Aaron Harun