basename() function for JavaScript

I wrote this basename() function for JavaScript today. It parses a file portion for either windows ‘c:pathtofile.ext’, *nix ‘/path/to/file.ext’ or href ‘http://server/path/to/file.ext’.

function GetBaseName(file)
{
var Parts = file.split('\');
if( Parts.length < 2 )
Parts = file.split('/');
return Parts[ Parts.length -1 ];
}

2 Responses to “basename() function for JavaScript”

  1. person Says:

    Thank you!

  2. David Nash Says:

    This was very helpful, thanks!

Leave a Reply