Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js function to get filename from url

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 letter filenames, with .html extension).

I want it to be a safe function, so if someone passes in an empty url it doesn't break.

I know how to check for length, but I should be checking for null to right?

like image 999
Blankman Avatar asked Feb 04 '09 15:02

Blankman


2 Answers

var filename = url.split('/').pop() 
like image 96
user2492653 Avatar answered Oct 12 '22 10:10

user2492653


Why so difficult?

var filename = url.split('/').pop().split('#')[0].split('?')[0];

like image 25
Quidn Avatar answered Oct 12 '22 11:10

Quidn