Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Link | Q1369 [ previous next ]

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

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);
}

Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.