Welcome to WebmasterWorld Guest from 54.226.62.26

Forum Moderators: open

Message Too Old, No Replies

Inserting the filename by JavaScript

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

     

quietus

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

10+ Year Member



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".

Thx

BlobFisk

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

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

HTH

toadhall

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

10+ Year Member



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.

T

quietus

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

10+ Year Member



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?

BlobFisk

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

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



<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);
document.write(myText);
</script>

toadhall

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

10+ Year Member



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.

T

BlobFisk

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

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

toadhall

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

10+ Year Member



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

T

tedster

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

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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.

toadhall

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

10+ Year Member



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

quietus

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

10+ Year Member



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

BlobFisk

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

WebmasterWorld Senior Member blobfisk is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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?

quietus

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

10+ Year Member



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

DrDoc

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

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

quietus

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

10+ Year Member



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

<SCRIPT>
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
</SCRIPT>

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

<SCRIPT>
document.write(DocName)
</SCRIPT>

 

Featured Threads

Hot Threads This Week

Hot Threads This Month