javascript - Current script path with type="module" - Stack Overflow

I'm trying to access a file that I want to reference relative to a script file while using <scr

I'm trying to access a file that I want to reference relative to a script file while using <script type="module">. Normally, without type="module", people seem to be looking up the script tag and using its src attribute to get the path, but this obviously isn't possible after using imports.

The scenario looks roughly like this:

File structure:

js/
    script.js
    other/
        imported.js
index.html

index.html:

<html><head><script type="module" src="js/script.js"></script></head></html>

script.js

import "other/imported.js";

imported.js

// ??? should with some code magic bee "js/other/imported.js" or similar
console.log("The path to this script is: " + "???");

I saw some thread somewhere where it was discussed that the reason document.currentScript is null when using type="module" is because they wanted to figure out a better alternative. I suppose this alternative is not yet available?

So, to recap, with the file structure above, how can one find the path to js/other/imported.js in that script dynamically?

I'm trying to access a file that I want to reference relative to a script file while using <script type="module">. Normally, without type="module", people seem to be looking up the script tag and using its src attribute to get the path, but this obviously isn't possible after using imports.

The scenario looks roughly like this:

File structure:

js/
    script.js
    other/
        imported.js
index.html

index.html:

<html><head><script type="module" src="js/script.js"></script></head></html>

script.js

import "other/imported.js";

imported.js

// ??? should with some code magic bee "js/other/imported.js" or similar
console.log("The path to this script is: " + "???");

I saw some thread somewhere where it was discussed that the reason document.currentScript is null when using type="module" is because they wanted to figure out a better alternative. I suppose this alternative is not yet available?

So, to recap, with the file structure above, how can one find the path to js/other/imported.js in that script dynamically?

Share Improve this question asked Jun 18, 2017 at 22:51 EllieEllie 2234 silver badges11 bronze badges 2
  • 1 Are there browsers that support module imports today? You probably need to say which browser you using to get a valid answer. – styfle Commented Jun 18, 2017 at 23:32
  • I was thinking to have the answer be at least somewhat standard pliant, but right now, I'm using chrome beta (60) to test and will probably verify with Firefox and maybe safari that everything works. – Ellie Commented Jun 19, 2017 at 9:00
Add a ment  | 

1 Answer 1

Reset to default 8

To answer my own question almost 2 years later, there's now a thing called import.meta which is used to provide arbitrary information about the current module. In this case, import.meta.url would be the thing I was looking for.

You can read more on this MDN page: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/import.meta

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645556a4637948.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信