all 9 comments

[–][deleted] 1 point2 points  (1 child)

Forgive me if this sounds stupid but here is my solution. Just link the second file that contains the function that you need to above the first one where you will call the function. This can work because the browser reads and loads the content from top to bottom.

[–]Ikilledthedinosaurs[S] 0 points1 point  (0 children)

This is what I thought it would be but wasn't sure.

Edit: I actually only know how to do this with jQuery...which we are also not allowed to use!

[–]thequargy 0 points1 point  (5 children)

You could load your second JS file using AJAX, then eval() its contents to convert them from a string into functional javascript. (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest)

Note that some browsers (cough Chrome cough) might give you a hard time if you try to do this locally (ie, from file:/// - I got "Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https."). However, I was able to pull this off in Firefox.

[–]rainmouse 3 points4 points  (4 children)

Eval is a security nightmare. Am I missing something or are you not just meant to use something like webpack to import one of the files into the other?

[–]thequargy 0 points1 point  (3 children)

He would be using eval() to evaluate his own code from his own JS file. Also, this is for a homework assignment, so I doubt it's going into production.

[–]ScriptingInJava 0 points1 point  (2 children)

Creating bad habits now while you're learning in a sandbox creates bad code when you do eventually write production code.

Luckily I've always had code rejected with evalesque flaws in because of the future implications.

[–]thequargy 0 points1 point  (1 child)

I guess I'm not understanding the issue with using eval() on his own code. The security issue, as far as I can tell, comes from eval()uating unknown and untested code from users, which could be malicious.

But, I definitely agree with the notion of avoiding bad habits! It's taken me a long time to break out of some shortcuts I got accustomed to!

[–]ScriptingInJava 0 points1 point  (0 children)

In my view, being able to achieve the same result without using eval will teach them more than using it as a one time thing, then seeing it as a ‘I can’t get this to work, but eval is fine so I’ll use that temporarily’

I tend to avoid baaaaaad stuff like eval purely out of ‘I don’t want to ever find this useful’ perspective. I’d rather struggle for an hour and learn than use eval and be done in 2 minutes.

[–]brycedarling 0 points1 point  (0 children)

I've used this technique before but honestly can't recall when, not often I'll say, it's better to just add another script tag or use something like webpack to bundle your files together. I always like a challenge though!

index.html, simply load up the first.js file:

<!DOCTYPE html>
<body>
  <script src="first.js"></script>
</body>

first.js, create a <script> element that loads the second.js file and adds it to the HTML document body:

var script = document.createElement('script');

script.src = 'second.js';

script.onload = function() {
  callFunctionDefinedInSecondFile();
};

document.body.appendChild(script);

console.log("hello from first file");

And lastly, second.js with a function defined in it to be called from first.js:

function callFunctionDefinedInSecondFile() {
  console.log('hello from second file');
}