2

How to declare a global variable using JavaScript, whose life remain through out the HTML code? Can we access the variable that we had declared in one script in another script?

6
  • 1
    Do you mean window.variable = 'value'? Commented Jul 11, 2011 at 17:35
  • @bestsss - I'd -1 that if I could. window. is valid, top. isn't as it can potentially go to a different frame. Commented Jul 11, 2011 at 17:40
  • @cwolves, sure it's that's the point of the global, i understand very well what top is. it also helps w/ iframes and so on. Commented Jul 11, 2011 at 17:53
  • @bestsss - If you declare a variable with top., it's only safely accessible using top., which is just a bad style in my opinion. Commented Jul 11, 2011 at 17:55
  • What if someone overwrites both window and top and self? Commented Jul 11, 2011 at 17:58

7 Answers 7

5

"Don't do this" is the simple answer. Clogging the global scope is generally a bad thing, especially if you have to ask how (which usually means that you're asking because you think it's the easy solution, but it's almost certainly not the right one). What exactly are you trying to do?

If you really want to, either:

  • declare it outside of any function
  • don't use the var keyword
  • use window.variable = value
Sign up to request clarification or add additional context in comments.

4 Comments

"don't use the var keyword" This will break in "strict mode", and likely non-strict mode in future versions of ECMAScript.
@patrick dw - true, but it's 10-15+ years away that doing so will break in the default mode of any browser.
In non-strict perhaps (hopefully not that long), but strict mode is today. Anyway, good to form the habits early. ;o)
@patrick dw - I agree with the "hopefully not that long" sentiment, but IE4/5/6 were released in '97,'99,'01 and most browsers/code still default to things that those browsers almost entirely understand. (btw, had MS stuck to that time-line we should have IE11 right about now, and it would be an amazing browser considering the jump from IE4 --> 6)
2

Declare a variable outside any function. It will be accessible in other scripts.

Comments

2

Global variables are declared by using either the var keyword outside of the scope of a function, by assigning a variable without using var, or by directly assigning a property of the window object.

<script>
var global1 = 'foo';
global2 = 'bar';
window.global3 = 'baz';
function f() {
    var not_global;
}
</script>

1 Comment

Please don't suggest excluding var as a means of creating a global. You'll get a ReferenceError if you're running your code in strict mode.
1

Declare a variable in a script tag before your other scripts.

<script type="text/javascript">
   var global = "hello world";
</script>

Comments

0

Declare your variable in a <script> tag, but make sure to place it within your <body> tag, or the browser may not execute it!

Alternatively you may use a cookie.

2 Comments

my php code that is written inside <?php some ?> is not executing...rest og the code which is html is executing..what could be the problem.My file is with .php extention
This seems like a different problem. My not executing, do you mean that if you look at the source code (after the file is placed on your server), you see your original PHP code? If so, then check that you indeed have PHP properly installed on your server.
0

Any variable that is defined outside a function or a class is global variable in Javascript.

For example:

<script>
    var itsAGlobalVariable;
    function someMethod() {
        var itsALocalVariable;
    }
</script>

Comments

0

You mean something like this:

   var Foo = {
       Bar: Value
   }

Then you can access to this like that:

Foo.Bar

You can also set values:

Foo.Bar = "fancy value"

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.