newguy newguy - 1 year ago 74
Node.js Question

My frontend javascript file cannot access variables in other javascript file

The problem is that I try to put my codes in separate javascript files. And one file will refer to an variable in a file which is referenced in

file previously.

For instance, the two files in my public folder are

In my
file, I have these codes:

function init() {
var socket = io.connect();
var temp = x;

document.addEventListener("DOMContentLoaded", init, false);

In my
file, I have these codes:

var x = 5;

In my
file, I have these codes:

<!DOCTYPE html>
<title>Amazing Particle System</title>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<canvas id="particle"></canvas>
<script src="/"></script>
<script scr="/test.js"></script>
<script src="/client.js"></script>

But when I run my code I always get the error:

client.js:15 Uncaught ReferenceError: x is not defined

In developer tool I cannot see my
file in sources tab.

Is there anything I have done wrong in my code? How can I fix the issue?

Answer Source

You have to correct a typo in <script scr="/test.js"></script>, you have wrote scr instead of src attribute.. That's why your script do not work as expected

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download