js function to get filename from url

前端 未结 19 2495
挽巷
挽巷 2020-11-30 03:01

I have a url like http://www.example.com/blah/th.html

I need a javascript function to give me the \'th\' value from that.

All my urls have the same format (2

相关标签:
19条回答
  • 2020-11-30 03:40

    Similarly to what @user2492653 suggested, if all you want is the name of the file like Firefox gives you, then you the split() method, which breaks the string into an array of components, then all you need to do it grab the last index.

    var temp = url.split("//");
    if(temp.length > 1)
     return temp[temp.length-1] //length-1 since array indexes start at 0
    

    This would basically break C:/fakepath/test.csv into {"C:", "fakepath", "test.csv"}

    0 讨论(0)
提交回复
热议问题