简体   繁体   中英

Why can't I access global-scope variables from one file in another file?

Say I have the following code in my HTML body:

<script src = "first.js"></script>
<script src = "second.js"></script>

So in first.js I have two variables declared in a global scope. Why can't I access them from second.js?

The following code works as expected. Without being able to see all your relevant code it's impossible to diagnose your issue. That's why you're being downvoted.

index.html

<script src="first.js"></script>
<script src="second.js"></script>
<script>
  console.log(firstGlobal, secondGlobal); // true true
</script>

first.js

var firstGlobal = true;

second.js

var secondGlobal = true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM