1

It's like this:

<script>
  var global_var = {foo:1};
</script>

<script src="myscript.js"></script>

in myscript.js I have

jQuery(document).ready(function($){    
  var global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
  console.log(global_var);    
});

But the global_var does not seem to be recognized in my 2nd script, even though I clearly defined it in the first, like a global variable.

http://jsfiddle.net/tYErg/

0

6 Answers 6

3

The problem is the "var" in "var global_var" within your jQuery function. This is creating a different "global_var" variable within your function instead of using the true global. Drop this and your code works as expected.

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

Comments

2

It's because you've declared another global_var in the current scope. Either remove the var or change the variable name:

eg

global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
var other_var = typeof global_var == 'undefined' ? {foo:2} : global_var;

http://jsfiddle.net/8Mptw/

Comments

2

You can't use two different variables with the same name from different scopes within the same scope. So when you declare your local global_var with the var keyword, your global becomes inaccessible.

If you need to make a local variable with the same name as a global you can use a closure like:

jQuery(document).ready(function($){
    (function(global_var){
        // global_var is a local copy of global_var from the outer scope
        global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
        console.log(global_var);
    })(global_var);    
});

You can also refer to it as a property of the window object if you know it's global:

jQuery(document).ready(function($){
    var global_var = typeof window.global_var == 'undefined' ? 
      {foo:2} : window.global_var;
    console.log(global_var);
});

Finally, if you don't want a local copy and just want to access the variable from the outer scope, then do not use the var keyword, since the purpose of that keyword is declaring a new variable:

jQuery(document).ready(function($){
    global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
    console.log(global_var);
});

Comments

1

Try defining it without the var key work to make it in the global scope

<script>
     var global_var = {foo:1};
    </script>   

<script>    
      jQuery(document).ready(function($){   
      global_var = typeof global_var === 'undefined' ? {foo:2} : global_var;    
      alert(global_var.foo); // should be 1    
    });        
</script>   

http://jsfiddle.net/tYErg/3/

Comments

1

try this

jQuery(document).ready(function($){    
 global_var = typeof global_var == 'undefined' ? {foo:2} : global_var;
  console.log(global_var);    
});

Comments

1

You are having this problem because you are declaring a local variable with the same name as your global variable, therefore the global variable is not visible any more. You can still access it by explicitly specifying the scope:

jQuery(document).ready(function($){    
  var global_var = typeof window.global_var == 'undefined' ? {foo:2} : window.global_var;
  console.log(global_var);    
});

Comments

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.