tags:

views:

52

answers:

3
<script>
//in one script
var someVarName_10 = 20;
</script>

I want to get access to this variable from another script by name of variable. With window object its simple, is it possible with local variable?

I mean access this var by code like this:

<script>
  alert(all_vars['someVar' + 'Name' + num]);
</script>
A: 

If this is what you said:

<script type="text/javascript">
var hello = 'test';
</script>
<script type="text/javascript">
  alert (hello);
</script>

It works because script are finally available to the document and you can access their vars.

Sarfraz
+1  A: 

Do you want to do something like this?

<script>
//in one script
var someVarName_10 = 20;

alert(window["someVarName_10"]); //alert 20

</script>

Update: because OP edited the question.

<script>
  num=10;
  alert(window['someVar' + 'Name_' + num]); //alert 20
</script>
S.Mark
sorry, I was wrong, i thought that vars are not in window, thanks
dynback.com
you're welcome.
S.Mark
A: 

Try

<script type="text/javascript">
var hello = 'test';
alert(window.hallo);
</script>

or

<script type="text/javascript">
var hello = 'test';
alert(window["test"]);
</script>

Where "test" may be any Stringname of a variable