Forum Moderators: open
e.g. [myhost.com...] i just want the script to return "testfile".
Thx
<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
//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>
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
Quietus, did that last script do the job for you?
<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>