0

I need to retrieve the file name of an HTM file - the file needs to retrieve its own file name - to use in another Javascript function within the same file. So far I have -

var Docname = "ESSA_CL_2009_01"
var DSstem = new Spry.Data.XMLDataSet("ESSA10_DA_sourceData_19_1.xml", "ESSA_CL_2009/" + Docname + "/Item_stem");

(the Spry or AJAX stuff is already set up)

The var Docname I'd like to be generated dynamically. It doesn't have a URL yet as such - I don't have any control over its final destination.

Any help would be much appreciated. Thanks.

3
  • what is ESSA_CL_2009_01 in that case? Commented Jan 19, 2010 at 0:21
  • ESSA_CL_2009_01 is the filename without .htm suffix if I were to do things manually. However I want to do things dynamically, so I want code to replace the first line of code I put in in the above. I'm thinking (contrary to what I first stated) it does still have a URL .. I just need the file name without the its preceding URL content and without the suffix. Commented Jan 19, 2010 at 0:45
  • Now as it happens a similar problem was discussed at webmasterworld.com/forum91/386.htm .. The code provided there to retrieve the filename from the URL was as follows: 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 document.write(Docname); For whatever reason this doesn't appear to work.. Commented Jan 19, 2010 at 0:47

2 Answers 2

1

If the "Docname" refers to the current page's static file path, then you can use document.location.pathname to get the pathname portion of the URL, and then parse that as a string to grab only the portion you desire.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks.. um - there won't be a static file path: The final destination location of the file will be different and is unknown.. Using var url=document.URL would seem to be be the right way to go as an initial line of code, rather than pathname (?) - but it doesn't work either .. (I did try var url=document.location.pathname;)
0
var getFileName = function (uri) {
  var fileName = uri.substr(uri.lastIndexOf("/") + 1);
  return fileName.substr(0, fileName.lastIndexOf("."));
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.