admin管理员组文章数量:1026406
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
1 Answer
Reset to default 8To 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
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
1 Answer
Reset to default 8To 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
本文标签: javascriptCurrent script path with typequotmodulequotStack Overflow
版权声明:本文标题:javascript - Current script path with type="module" - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745645554a2161002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论