-1

I am trying to pass 4 variables from one js file to another.

I read here that you must write:

window.myVar = "foo";

to make your variable "super"-global.

In the first js file, I have

window.signJoueur1 = string1.charAt(7);  
window.signJoueur2 = string2.charAt(7);  
window.valeurJoueur1 = random1;  
window.valeurJoueur2 = random2;  

In the second js file, I did

console.log(window.signJoueur1);
console.log(window.signJoueur2);
console.log(window.valeurJoueur1);
console.log(window.valeurJoueur2);

function trouveCombinaison(signJoueur1, signJoueur2, valeurJoueur1, valeurJoueur2)
{
console.log(signJoueur1);
console.log(signJoueur2);
console.log(valeurJoueur1);
console.log(valeurJoueur2);
}

It should work, but all console.log return `undefined'.

If you want more informations here are the full codes:
first .js http://pastebin.com/0zJKFNem
second .js http://pastebin.com/TsWc7TxL
the html http://pastebin.com/t3SzwZSC

So, my question is, how can I actually pass through the variables?

3
  • Please don't just add a '2' to the title to allow your question to be posted. Come up with a more meaningful title. Commented Jan 4, 2015 at 12:02
  • in trouveCombinaison, it passes as arguments, so that will comes under scope of trouveCombinaison. in that variables has value undefined Commented Jan 4, 2015 at 12:03
  • @George i took into account your request, thank you. Commented Jan 4, 2015 at 12:04

2 Answers 2

2

You are trying to use the values before they exist.

The code that assigns the values to the variables are inside a function, and that function isn't called until you click a button. The code that tries to show the values is executed when the page loads, so it uses the variables before they have been assigned any values.

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

Comments

0

Actually i just had to put window. in the second console.log group.

Such as :

function trouveCombinaison()
{
console.log(window.signJoueur1);
console.log(window.signJoueur2);
console.log(window.valeurJoueur1);
console.log(window.valeurJoueur2);
}

The fact that the console.log out of the function don't work is that it's executed when the page loads, as explained by @Guffa.

Now it works.

1 Comment

Why do you even need arguments to that function? They do nothing.

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.