在javascript中获取最后一段网址

时间:2014-05-15 23:15:50

标签: javascript

我正在尝试使用下面的代码来提取网址中的最后一个细分,但它似乎不适用于下面显示的网址。有人可以检查下面的代码并告诉我这里缺少什么?感谢

   var segment_str = window.location.pathname; 
   var segment_array = segment_str.split( '/' );
   var last_segment = segment_array[segment_array.length - 1];
   alert(last_segment);

URL

   http://localhost/projects/myproject/app/#/sales/invoices/224

我只需要提取224

3 个答案:

答案 0 :(得分:4)

该网址中有#,因此" 224"是location hash的一部分,而不是pathname

var last_segment = window.location.hash.split('/').pop();

答案 1 :(得分:1)

尝试window.location.hash获取#之后的细分,然后将其拆分。

答案 2 :(得分:1)

正如Alan Wu所说,使用window.location.hash来获取哈希后的内容:

var segment_str = window.location.hash; 
var last_part = segment_str.substr(segment_str.lastIndexOf('/') + 1)

会给你224

相关问题