2

How to declare a variable, I think global, the way I declare in an html file and then use it in a js file (included by <script> tags)?

4 Answers 4

9

You can assign to the window object, i.e. window.myGlobal = 3;. window is the default context for variable binding. That's why you can reference document instead of needing to do a window.document.

But yeah as David says, you should avoid using globals. And if you are going to use globals, you should place them and other top-level declarations in a "namespace" object to avoid potential naming collisions with other libraries, like this:

myNamespace = { myGlobal: 3 };

// Then to access...
myNamespace.myGlobal = 6;
6

Don't use the var keyword

(That said, globals are usually the wrong solution to any given problem in JS)

3
  • 2
    Or any other language, for that matter Aug 10, 2010 at 22:55
  • 3
    However: do use the var keyword. If you use a variable without declaring it var anywhere, then yes, you get an accidental global, but it's not valid in ECMAScript Fifth Edition's Strict Mode, and if you are unlucky enough to use a name that matches any named element (ie. any with id, and in some cases name), IE will give you an error when you try to assign to it. You can declare a variable var in global context (instead of inside a function) to make it a global, and if you are using a global, you definitely should do that.
    – bobince
    Aug 10, 2010 at 23:22
  • @bobince: You've stolen my words! :), under strict mode an assignment to an undeclared identifier will throw a ReferenceError exception... Aug 10, 2010 at 23:31
5

So as I understand, you want to use a variable from an HTML file in a JS file? To pass a variable from an HTML file to a javascript file, pass it with a function:

HTML.html

    <a href="#" onClick="test('John Doe')">Send Name</a>

Javascript.js

function test(full_name) {
 alert(full_name);
}
4
  • uuuu, i like your solution... :)
    – richardaum
    Aug 10, 2010 at 22:57
  • Thanks! I hope this answered your question
    – Jon McIntosh
    Aug 10, 2010 at 23:00
  • Variables cannot have a dash in the variable name, so full-name is not valid. You can try either full_name, fullName, or something else. The problem is that the JavaScript parser will try to subtract the 'full' variable from the 'name' variable.
    – Buddy
    Aug 10, 2010 at 23:12
  • Doh! Good catch, I often find myself doing that.
    – Jon McIntosh
    Aug 10, 2010 at 23:15
2

Please, avoid using global variables.

To answer your question, there are two ways of declaring a global variable in JavaScript. You can either omit the 'var' keyword, or declare the variable outside any function.

In this code sample, both thisIsGlobal and thisIsAlsoGlobal are global variables and set to null.

var thisIsGlobal= null;
function foo() {
    thisIsAlsoGlobal = null;
    // code goes here
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.