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 comments on "basename() function for JavaScript"

  1. David Nash says:

    This was very helpful, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *