Get filename from url


/ Published in: JavaScript
Save to your folder(s)

Strips the filename from a url, returns null if one is not found


Copy this code and paste it in your HTML
  1. utils.getFilenameFromUrl = function(url) {
  2. var charPosition = url.length;
  3. var positionFirstSlash;
  4. var currentCharacter = url.charAt(charPosition);
  5. var breakCharacter = '/';
  6. var filename;
  7. if (currentCharacter != breakCharacter) {
  8. while(currentCharacter != breakCharacter) {
  9. currentCharacter = url.charAt(charPosition);
  10. positionFirstSlash = charPosition;
  11. charPosition--;
  12. }
  13. filename = url.substr(positionFirstSlash + 1, url.length);
  14. }
  15. return filename;
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.