homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Inserting the filename by JavaScript
I need help in finding a script that can take the filename of the file

 4:22 pm on May 1, 2003 (gmt 0)

How can I make a script that extracts the filename of the active file the script is in, and inserting this filename into the document?

e.g. [myhost.com...] i just want the script to return "testfile".




 4:45 pm on May 1, 2003 (gmt 0)

Welcome to WebmasterWorld, quietus.

A script can read the URL of the page, using location.href.

Then, find the last instance of '/' and the .html, and everything in between is the string that you are looking for.



 5:07 pm on May 1, 2003 (gmt 0)

If you have no objection to naming the page by the file's name, ie,
<title>foobar</title> ...for foobar.html, then a quick and clean way of gaining the file's name would be document.write(document.title);

And if you do have an objection, then you have something to add to your "nifty javascript tricks" folder.



 5:28 pm on May 1, 2003 (gmt 0)

I'm thankful for giving me a hint on using location.href, but I'm very new at this, can you type up an example?


 4:26 pm on May 2, 2003 (gmt 0)

<script type="text/JavaScript">
myUrl = location.href;
mySub1 = "/";
mySub2 = ".html";

//Returns everything right of the last instance of the subString to the end of the fullString
function rightFromSubStringToEndOfFullString(fullString, subString) {
if (fullString.lastIndexOf(subString) == -1) {
return "";
} else {
return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);

//Returns everything left of the last instance of the subString to the start of the fullString
function leftFromSubStringToBeginningOfFullString(fullString, subString) {
if (fullString.lastIndexOf(subString) == -1) {
return "";
} else {
return fullString.substring(0, fullString.lastIndexOf(subString));

//Strip out everything before the last "/"
myVar = rightFromSubStringToEndOfFullString(myUrl, mySub1);

//Strip out the .html
myText = leftFromSubStringToBeginningOfFullString(myVar, mySub2);


 5:04 pm on May 2, 2003 (gmt 0)

If you're using this in a place where you have control over the file extensions then:

var url = document.URL;
document.write(url.substring(url.lastIndexOf("\/")+1,url.length-5); // for *nix
document.write(url.substring(url.lastIndexOf("\\")+1,url.length-5); // for windows

...will handle a 4 letter file extension, like html. If you're using 3 letter extensions (htm) just change the -5 to -4.

Also, using document.URL will always give you the filename of the page the visitor landed on; location.href could possibly give the filename of a page that redirected the visitor.



 5:11 pm on May 2, 2003 (gmt 0)

Toadhall, that is a much nicer way of doing it!


 5:49 pm on May 2, 2003 (gmt 0)

Thanks BlobFisk.

Now that I've taken the time here's a better way. It will lob off any extension:

document.write(url.substring(url.lastIndexOf("\\")+1,url.lastIndexOf("\.")) // for windows

document.write(url.substring(url.lastIndexOf("\/")+1,url.lastIndexOf("\.")) // for *nix



 6:41 pm on May 2, 2003 (gmt 0)

I've enjoyed watching you perfect this script. It's the kind of thing that brings me pleasure -- I often put in ridiculous amounts of time perfecting things that already do the job, but just are not as elegant as I would like.

You folks are two after my own heart.


 8:56 pm on May 2, 2003 (gmt 0)

Thanks for that Tedster. Good to hear.
Doing the most with the least is gratifying.


 11:27 am on May 3, 2003 (gmt 0)

Thanks a million to both Blobfisk and toadhall, you're awesome :-)


 7:59 pm on May 3, 2003 (gmt 0)

Thanks Tedster. I have a tendancy to write scripts, keep going with what I'm doing and then come back later to try and trim them down! However, I've got into the habit of starting with quite long, simplistic code. For me it's easier to get to a solution that way and then come back and trim, trim and trim some more!

Quietus, did that last script do the job for you?


 7:29 am on May 4, 2003 (gmt 0)

Nah, not really.. on my pc it works fine, but online the script returns the complete file/path


 4:39 pm on May 4, 2003 (gmt 0)

Change the ("\\") section to ("\/")


 5:09 pm on May 4, 2003 (gmt 0)

That did it. Thanks, here's the final thingy (with a small addition of mine):

var url = document.URL;
var DocName =(url.substring(url.lastIndexOf("\\")+1,url.lastIndexOf("\."))) // for windows
var DocName =(url.substring(url.lastIndexOf("\/")+1,url.lastIndexOf("\."))) // for *nix

..and then use this for every instance you need the filename (if more than once)


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved