Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access a variable from another file in Javascript?

Let's say that I have two files that are in separate directories:

/folder1/first.js

/folder1/folder2/second.js

first.js contains a variable called newName. Would it be possible to access it in second.js? (and how)?

EDIT: The HTML file is located in the same directory as first.js.

like image 431
wh1te_d1ce Avatar asked Jun 02 '19 02:06

wh1te_d1ce


1 Answers

Assuming you are just linking static HTML files with <script> tags, then they will share their global scope. In other words: yes, second.js will be able to access the top-level variables from first.js, assuming they were liked in that order. Directories have no bearing on the behavior.

like image 58
Slava Knyazev Avatar answered Oct 06 '22 01:10

Slava Knyazev