Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q1369 How can I easily extract the filename from a URL?

You are here: irt.org | FAQ | JavaScript | Link | Q1369 [ previous next ]

The following was supplied by Joe Barta:

To extract the filename only (no extension) try this...

function DocNameExtract()
{
   wholeurl = window.location.href;
   x = wholeurl.length;
   while((wholeurl.substring(x,x-1)) != "."){ x--; } clipend = x;
   while((wholeurl.substring(x,x-1)) != "/"){ x--; } clipstart = x;
   return wholeurl.substring(clipend-1,clipstart);
}

The function returns the file name. You can then insert it into an alert box or whatever...

alert(DocNameExtract());

To extract the filename with the extension try this...

function DocFileNameExtract()
{
   wholeurl = window.location.href;
   x = wholeurl.length;
   while((wholeurl.substring(x,x-1)) != "/"){ x--; } clipstart = x;
   return wholeurl.substring(wholeurl.length,clipstart);
}

©2018 Martin Webb