basename() function for JavaScript

I wrote this basename() function for JavaScript today. It parses a file portion for either windows ‘c:\path\to\file.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 ];
}

Share and Enjoy:
  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

2 Responses to “basename() function for JavaScript”

  1. person Says:

    Thank you!

  2. David Nash Says:

    This was very helpful, thanks!

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word