views:

75

answers:

2

I have a script tag with 'A' variable and has been initialised as 16 but when I use alert on IE under onclick event, I received undefine rather than 16.

Is this the problem in IE 6?

e.g.

var A =16;

undefine

A: 

You need to post an example of what you are talking about, but the problem may be in where your script tag is and where you are calling the variable from.

qw3n
@qw3n - Put comments like this as a comment under the question, not as an answer.
Gert G
@Gert G Didn't I give at least half an answer. :)
qw3n
@qw3n - A very vague one. And for the record... I didn't give you a -1.
Gert G
+1  A: 

I'd bet on it being caused by some slight confusion...

Being that putting var in front of a variable puts that variable into the local scope only in Javascript, and a variable delcaration without it (a = 16) is global.

function test() {
    a = 16;
    var b = 16;

}
test();

alert(a); # alerts 16
alert(b); # throws error, b is undefined
corrodedmonkee
I'm a bit confused as to why exactly this has been down voted.
corrodedmonkee
Just to add, just looked it up. What I have said is correct, with the exception that if I had var c = 16, and the relevant alert inside test() it would have worked.Irrespective, it sounds like a scope problem.
corrodedmonkee
+1 for useful information in trying to answer a rather vague question (don't know why you were voted down either). It could well be a scope issue; it could also be a typo! ;)
w3d