andrew
andrew

Reputation: 1

Retrieving file name with Javascript (without URL?)

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.

Upvotes: 0

Views: 532

Answers (2)

Eli Grey
Eli Grey

Reputation: 35903

var getFileName = function (uri) {
  var fileName = uri.substr(uri.lastIndexOf("/") + 1);
  return fileName.substr(0, fileName.lastIndexOf("."));
};

Upvotes: 0

Ed Schembor
Ed Schembor

Reputation: 8560

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.

Upvotes: 1

Related Questions