I have no control how a 3rd party loads my scipt.
Can be <script type="module" src="myscript.js">
or <script src="myscript.js">
When not loaded as “module” the code inside my script always errors with:
Uncaught SyntaxError: Cannot use ‘import.meta’ outside a module
This SO answer “Uncaught SyntaxError: Cannot use import statement outside a module” when importing ECMAScript 6
is not an answer for native/vanilla JavaScript without bundling.
Is it possible at all to test for “module” inside a non-module script?
A simple if
does not work:
let myURL = "";
if (document.currentScript) {
myURL = document.currentScript.src;
} else {
myURL = import.meta.url;
}
Try-Catch does not work:
let myURL = "";
try {
myURL = import.meta.url;
} catch (e) {
if (document.currentScript) myURL = document.currentScript.src;
}
You-know-who and You-know-Elon-who come up with variations of above code that do not work, causing the error