1

I know that using a function or a variable by file1 from file2 can't work:

<head>
<script src="file1.js"></script>
<script src="file2.js"></script>
</head>

Because file2 is 1 line lower than file2, is there something that can make it possible? I don't want to use all of js script in one file or in html file because it would be a mess is there something i can do so it will work

I tried swapping file1 and file2 but it still gave me errors about functions and values being null and i don't know what to do

3
  • 1
    All scripts share the same global scope. If some definition is not in global scope, then it’s not accessible from outside. That being said, have you tried using modules instead? Commented Nov 24, 2022 at 18:27
  • 1
    Your script tags are wrong. It should be src="file1.js" and src="file2.js". Commented Nov 24, 2022 at 18:28
  • The point is that taken separately they are not supposed to depend on each other if there’s nothing making it explicit but when run together in the same context because bound as resources from the web page, that code will work because as said above in Javascript it’s a matter of scope at runtime. Anyway in terms of clear dependencies, it would be a bad choice to write things tightly coupled in separate files. That’s unless you use modules that make dependency explicit by packaging code. What I said here isn’t the Bible ofc. Take jquery for example. Your code may depend on it just make it expl Commented Nov 24, 2022 at 18:33

1 Answer 1

1

You don't "use" them in your HTML. you need to import one of them to the other, then you will have access to the other's values. for more information please follow the Link.

just a side note regarding your code. A. your script tags are wrong (should be <script src="file1"></script>) B. it's better to put your scripts at the bottom of your body element.

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

11 Comments

“it's better to put your scripts at the bottom of your body element” — That’s some old practice to guarantee that the DOM is ready before any script runs and to avoid blocking the HTML parser. Modules or scripts with the defer or async attribute solve both of these problems much better.
I agree but i don't think that the objective of the question...
Can is export functions and variables at the same time? And if a exported variable is changed and gets imported is it still changed?
@farciarzfunny You can have as many export statements as you want and export what you want. A module is identified by its module specifier (e.g. its URL); as long as you use the same specifier, a module will only be executed once. If you export a variable referencing an object, then yes, changing it anywhere will cause the object to change, and these changes will be reflected everywhere (there’s only one object since a module is only executed once).
@SebastianSimon can i import something from a js file into html file using <script> tag? ( It doesn't work without importing )
|

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.