Welcome to WebmasterWorld Guest from 54.196.244.206

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

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

New User

10+ Year Member

joined:May 1, 2003
posts:17
votes: 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".

Thx

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

Senior Member

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

joined:Feb 25, 2002
posts:3185
votes: 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.

HTH

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

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 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.

T

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

New User

10+ Year Member

joined:May 1, 2003
posts:17
votes: 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)

Senior Member

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

joined:Feb 25, 2002
posts:3185
votes: 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);
document.write(myText);
</script>

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

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 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.

T

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

Senior Member

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

joined:Feb 25, 2002
posts:3185
votes: 0


Toadhall, that is a much nicer way of doing it!
5:49 pm on May 2, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 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

T

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

Senior Member

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

joined:May 26, 2000
posts:37301
votes: 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)

Preferred Member

10+ Year Member

joined:May 9, 2001
posts:416
votes: 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)

New User

10+ Year Member

joined:May 1, 2003
posts:17
votes: 0


Thanks a million to both Blobfisk and toadhall, you're awesome :-)
7:59 pm on May 3, 2003 (gmt 0)

Senior Member

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

joined:Feb 25, 2002
posts:3185
votes: 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)

New User

10+ Year Member

joined:May 1, 2003
posts:17
votes: 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)

Senior Member

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

joined:Mar 15, 2002
posts:6807
votes: 0


Change the ("\\") section to ("\/")
5:09 pm on May 4, 2003 (gmt 0)

New User

10+ Year Member

joined:May 1, 2003
posts:17
votes: 0


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>

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members