最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论