0

I would like to define a variable, which will include my filename only without any extension. The example of my file is linked to the index.html page and it looks like this:

   <script src="js/20231014.js" type="text/javascript"></script>
   <script src="js/20211204.js" type="text/javascript"></script>
   <script src="js/20230423.js" type="text/javascript"></script>
  • about 50 more files like these

so I want in my variable only the "20231014" extracted.

I found some nice solutions here:

How to get the file name from a full path using JavaScript?

from where I tried:

    var filename = fullPath.replace(/^.*[\\\/]/, '')

but I got an error, that fullPath is not defined. That has led me to this link,

where I tried:

   var fullPath = FileSystemEntry.fullPath;

but unfortunately, the error just changed, that fileSystemEntry is not defined.

My another approach was:

   var fileurl = '/js/'.replace(/\.[^.]+$/, '')

where I got nothing

and finally

var fileurl = window.location.pathname;

where I got just index.html instead

Is there any swift solution for extracting the given filename from the link based in the other directory?

Geographos
  • 827
  • 2
  • 23
  • 57
  • You really arn't making a lot of sense. Since your script block is hardcoded in the html, you can also simply define your variable with the same hardcoded name? `var filename = 20231014`. The error you get in "fullPath is not defined", is probably because you've not defined your variable anywhere? Please show more code of what you're trying, also please explain more precisely what you're trying to obtain. – Anders Metnik Nov 02 '21 at 10:33
  • `fullPath` is your target file path. Also maybe you are looking for: [how to get path/url from script](https://stackoverflow.com/questions/2976651/javascript-how-do-i-get-the-url-of-script-being-called). Then you can extract filename using something like `'asdfsdf/js/20231014.js'.split(/(\\|\/)/).pop().split('.').shift()` – Xeelley Nov 02 '21 at 10:39
  • Ok, but if I have 50 .js files like this? How to make something versatile like adsdf/js/...js? – Geographos Nov 02 '21 at 10:54

1 Answers1

2

This code will do what you're after.

  • First we find the position of the last / character appearing in the file-path.
  • Then we get the string that appears after the last / in the path.
  • Next we replace the '.js' part with nothing.

I've only performed a single check. That avoids printing an empty string for the script element that does all the work, since it's in the document and doen's have a source.

You'll need to add error-checking before this code is any good.

<head>
  <script src="js/20231014.js" type="text/javascript"></script>
  <script src="js/20211204.js" type="text/javascript"></script>
  <script src="js/20230423.js" type="text/javascript"></script>
  <script>
    "use strict";
    window.addEventListener('load', onLoad, false);

    function onLoad(evt) {
      let scripts = document.querySelectorAll('script');
      scripts.forEach(scr => {
        if (scr.src != '') {
          let slashPos = scr.src.lastIndexOf('/');
          let filename = scr.src.slice(slashPos + 1);
          filename = filename.replace('.js', '');
          console.log(filename);
        }
      });
    }
  </script>
</head>
enhzflep
  • 12,927
  • 2
  • 32
  • 51