tags:

views:

62

answers:

4

This is my code:

 function insert(){
  var loc_array = document.location.href.split('/');
  var linkElement = document.getElementById("waBackButton");
  var linkElementLink = document.getElementById("waBackButtonlnk");
  linkElement.innerHTML=loc_array[loc_array.length-2];
  linkElementLink.href = loc_array[loc_array.length];
 }

I want linkElementLink.href to grab everything but the last item in the array. Right now it is broken, and the item before it gets the second-to-last item.

+3  A: 

I’m not quite sure what you’re trying to do. But you can use slice to slice the array:

loc_array = loc_array.slice(0, -1);
Gumbo
I want to take the current pages URL and just strip away the last one or two directores/pages instead of /m/fares/pass/trans.html ill get m/fares/
Bry4n
@Bry4n: Well, you can use `slice` to do that. Just adjust the second parameter with the number of items you want to remove.
Gumbo
A: 

linkElementLink.href = loc_array[loc_array.length]; adds a new empty slot in the array because arrays run from 0 to array.length-1; So you returning an empty slot.

linkElement.innerHTML=loc_array[loc_array.length-2]; if you use the brackets you are only getting the contents of one index. I'm not sure if that is what you want? The next section tells how to get more than one index.

To get what you want you need for the .href you need to slice the array. linkElementLink.href = loc_array.slice(0,loc_array.length-1); or using a negative to count from the end linkElementLink.href = loc_array.slice(0,-1); and this is the faster way.

Also note that when getting to stuff straight from the array you will get the same as the .toString() method, which is item1, item2, item3. If you don't want the commas you need to use .join(). Like array.join('-') would return item1-item2-item3. Here is a list of all the array methods http://www.w3schools.com/jsref/jsref_obj_array.asp. It is a good resource for doing this.

qw3n
I'm working in my local but the above script does thisfile:///C:/Documents%20and%20Settings/klgp/Desktop/Mobile/,,,C:,Documents%20and%20Settings,klgp,Desktop,Mobile
Bry4n
file:///C:/Documents%20and%20Settings/klgp/Desktop/Mobile/ this is your input what did you want for you output?
qw3n
A: 

Depending on whether or not you are ever going to reuse the array you could simply use the pop() method one time to remove the last element.

g.d.d.c
A: 

Wow, a lot of questions on this subject today. Use pathname in preference to href to retrieve only the path part of the link. Otherwise you'll get unexpected results if there is a ?query or #fragment suffix, or the path is / (no parent).

linkElementLink.href= location.pathname.split('/').slice(0, -1).join('/');

(But then, surely you could just say:)

linkElementLink.href= '.';

Don't do this:

linkElement.innerHTML=loc_array[loc_array.length-2];

Setting HTML from an arbitrary string is dangerous. If the URL you took this text from contains characters that are special in HTML, like < and &, users could inject markup. If you could get <script> in the URL (which you shouldn't be able to as it's invalid, but some browser might let you anyway) you'd have cross-site-scripting security holes.

To set the text of an element, instead of HTML, either use document.createTextNode('string') and append it to the element, or branch code to use innerText (IE) or textContent (other modern browsers).

bobince
You have an example of the latter about appending and ridding myself of innerhtml
Bry4n
I placed the item in brackets in in a node then appended it.
Bry4n
One way would be `element.innerHTML='';` (to clear it, first, if it's not already empty), and then `element.appendChild(document.createTextNode(somestring));`.
bobince