views:

2944

answers:

5

Hi

I need a few global variables that i need in all .js files.

For eg. consider the following 4 files

1) global.js
2) js1.js
3) js2.js
4) js3.js

Is there a way that i can declare 3 global variables in global.js and access them in any of the other 3 .js files considering I load all the above 4 files into a html doc

Can someone please tell me if this is possible or is there a work around for the same.

Thanks in Advance
Kartik

+2  A: 

Have you tried it?

If you do:

var HI = 'Hello World';

In global.js. And then do:

alert(HI);

In js1.js it will alert it fine. You just have to include global.js prior to the rest in the HTML document.

The only catch is that you have to declare it in the window's scope (not inside any functions).

You could just nix the var part and create them that way, but it's not good practice.

Paolo Bergantino
+6  A: 

Just define your variables in global.js outside a function scope:

// global.js
var global1 = "I'm a global!";
var global2 = "So am I!";

// other js-file
function testGlobal () {
    alert(global1);
}

To make sure that this works you have to include/link to global.js before you try to access any variables defined in that file:

<html>
    <head>
        <!-- Include global.js first -->
        <script src="/YOUR_PATH/global.js" type="text/javascript"></script>
        <!-- Now we can reference variables, objects, functions etc. 
             defined in global.js -->
        <script src="/YOUR_PATH/otherJsFile.js" type="text/javascript"></script>
    </head>
    [...]
</html>

You could, of course, link in the script tags just before the closing <body>-tag if you do not want the load of js-files to interrupt the initial page load.

PatrikAkerstrand
While this answer is correct I would recommend that you Google Javascript variable scoping to get a better understanding and possibly avoid doing things this exact way.
aleemb
Agreed. I always try to scope all functions and variables in a common "namespace" to avoid cluttering and conflicts. Usually I name it as an abbrevation of the project or company.
PatrikAkerstrand
+1  A: 

Yes you can access them. You should declare them in 'public space' (outside any functions) as:

var globalvar1 = 'value';

You can access them later on, also in other files.

Ropstah
A: 

The recommended approach is:

window.greeting = "Hello World!"

You can then access it within any function:

function foo() {

   alert(greeting); // Hello World!
   alert(window["greeting"]); // Hello World!
   alert(window.greeting); // Hello World! (recommended)

}

This approach is preferred for two reasons.

  1. The intent is explicit. The use of the var keyword can easily lead to declaring global vars that were intended to be local or vice versa. This sort of variable scoping is a point of confusion for a lot of Javascript developers. So as a general rule, I make sure all variable declarations are preceded with the keyword var or the prefix window.

  2. You standardize this syntax for reading the variables this way as well which means that a locally scoped var doesn't clobber the global var or vice versa. For example what happens here is ambiguous:

 

 greeting = "Aloha";

 function foo() {
     greeting = "Hello"; // overrides global!
 }

 function bar(greeting) {
   alert(greeting);
 }

 foo();
 bar("Howdy"); // does it alert "Hello" or "Howdy" ?

However, this is much cleaner and less error prone (you don't really need to remember all the variable scoping rules):

 function foo() {
     window.greeting = "Hello";
 }

 function bar(greeting) {
   alert(greeting);
 }

 foo();
 bar("Howdy"); // alerts "Howdy"
aleemb
A: 

Dear Friend

for this code not working

...
var adelm = 0;
_selectDate(1,2,3,4,5);
_subformat(1,2,3,4,5,6);
function _subformat(a0,a1,a2,a3,a4,a5,a6)
{
alert("Check"+adelm +" "+top.adelm +" "+this.adelm );
}
function _selectDate(a0,a1,a20,a21,a22)
{
adelm=30;
}

how can i solve it ? Regards Adel

Adel
You should post this as it's own question as this one has already been answered.
Ian Devlin