Get the last item in an array

前端 未结 30 3058
执念已碎
执念已碎 2020-11-22 05:28

Here is my JavaScript code so far:

var linkElement = document.getElementById(\"BackButton\");
var loc_array = document.location.href.split(\'/\');
var newT =         


        
30条回答
  •  轮回少年
    2020-11-22 05:46

    I'd rather use array.pop() than indexes.

    while(loc_array.pop()!= "index.html"){
    }
    var newT = document.createTextNode(unescape(capWords(loc_array[loc_array.length])));
    

    this way you always get the element previous to index.html (providing your array has isolated index.html as one item). Note: You'll lose the last elements from the array, though.

提交回复
热议问题