0

I have 2 JS files, I need to pass 2 variables from one to the other. For that I declared the variables as global in B.js and just tried to change them from A.js. This didn't work. (I have referenced both files correctly in the html). Now I'm trying to change the vars from B.js by calling a function from A.js and passing the vars as parameters.

B.js:

var re = '';
var un = '';

function init(response_string, username_string) {
   re = response_string;
   un = username_string;
}

A.js:

init(response, un);

The function in B.js gets the parameters correctly, however I can't seem to find a way to change the global variables. They remain as "".

EDIT: The values I parse from A.js are non-empty strings. I also tried this: init('test', 'test'); But the global variables still weren't affected.

EDIT 2: Heres more code: index.html:

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css">
<script src="A.js"></script>
<script src="B.js"></script>

</head>

<body>

<input type="text" name="" id="inp">
<a href="" onclick="pass()">Go</a>


</body>

</html>

A.js:

function pass() {
var text = document.getElementById('inp').value;
pass2(text);
}

B.js:

var global_string = '';

function pass2(v) {
console.log(v);
global_string = v;
}
5
  • 3
    Show us a minimal reproducible example that reproduces problem. What you have shown has one undefined variable and the other will set itself with empty string Commented Apr 6, 2019 at 16:28
  • un never changes. What's response? Commented Apr 6, 2019 at 16:29
  • Are there any asynchronous operations like ajax involved? without more code context what you are showing should work fine when proper values passed to init() Commented Apr 6, 2019 at 16:40
  • To be honest, I'm very new to js so I'm not 100% sure. But I can tell you that the relevant part is pure js. Commented Apr 6, 2019 at 16:41
  • 1
    Then show us more code context ... where you set it and where you try to access those variables after you set them. Numerous reasons you could be running into problems and we can't guess Commented Apr 6, 2019 at 16:42

1 Answer 1

1

You're calling init(response, un) with two variables as parameters, but the first variable response has not been defined, and the second variable un, you've defined as an empty string '' at the beginning of the code.

Call init() with two non-empty strings, and you'll see that the global variables un and re get updated correctly.

var re = '';
var un = '';

function init(response_string, username_string) {
   re = response_string;
   un = username_string;
}

init("test1","test2");
console.log(re);
console.log(un);

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

3 Comments

I suspect OP has a synchronicity issue also
They are both non-empty strings. I can even do console.log(response_string) in the init() function and I get the correct string as output, I just can't update the global variables.
@b3nj4m1n you haven't posted enough code for others to be able to reproduce and thus solve the issue.

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.