0

I'm trying to load another JS file from JS (because the url can change depending on what the user selected and load both would cause conflict issues) and then right after loading the file run a function from the file. I need to run a function and I can't just run code because the function requires client inputs. How can I do this?

In this example, I haven't included the dynamic URL part because that works. Also, no 2 attempts were tried at once. I tested them all separately. None of them worked. This is what I've tried:

var url="file.js", script = document.createElement('script');
script.setAttribute('src',url);
script.setAttribute('id','option');
document.head.appendChild(script);

// code attempt 1 at loading the function:
fileInit(param1);

// code attempt 2:
document.getElementById("option").onload = fileInit(param1);

// code attempt 3:
script = document.getElementById("option");
script.onload = script.onreadystatechange = fileInit(param1);

// code attempt 4:
document.getElementById("option").addEventListener("load", fileInit(param1));

I just want to load the file is JS (so I can have a dynamic url) and then when the file is loaded run the init function defined in the file. I also don't want to use jQuery. I want the code to be vanilla JS. I know you can use jQuery.getScript().

4
  • I've done something similar using a php file and cookies to determine which js library to return. Commented Sep 6, 2019 at 17:06
  • I looked around for a bit and load library isn't a javascript function @ControlAltDel Commented Sep 6, 2019 at 17:30
  • Are you trying to do something like JSONP here? Commented Sep 6, 2019 at 18:00
  • No JSON. I really want to load an options script. The thing I'm working on is kind of complex, this is just really specific to my needs. I can give you a link to the GitHub repository if you really want it @scottheckel Commented Sep 6, 2019 at 18:38

1 Answer 1

1

In both files you should have this statement exports.__esModule = true; and in the export file you do this for each thing you want to export exports.thingToExport = thingToExport; then you import like so:

var file = require("path/to/file");
file.thingToExport;

Edit: This question explains what you do in typescript to get this node code.This also works in modern browsers import functions from another js file and is probably a duplicated.

Sign up to request clarification or add additional context in comments.

7 Comments

Ok,I hope that works if Im rigth it is ES5 and earlier compatible.
I looked into it, and that's not normal JavaScript. It's TypeScript, I think. exports is undefined.
Yes,I compiled from TS but I thougth that TS replicate ES5 javascript code(unless you specify another) and this works perfectly on node.
Browsers like chrome use ES6
Right. Got, I'm just using import with type="module" in the script tag
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.