Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi all,
quick question re: javascript. i am writing out javascript variables from server-side code. then, i "include" a javascript file with all the necesary functions to work with those variables. however, it doesn't seem like my variables are available in the "global" scope. is there anway i can make variables global within a page context? see below...
--- start page ---<write out html>
<write out javascript variables from server>
<include javascript....functions.js with functions to work with variables above>
<end html output>
--- end page ---how can i make the variables available to the functions in an 'include' file? I'm sure it's something simpke, but i can't find it! i thought with the include and the variables, it would appear as if everything was within one script.
Thanks in advance.

Oh, a bit more information. For the "variables" I mentioned aobve, I did mark them with var. They are also an array of an object I created....
<start psuedo script>
var myvariable = new Array(2)
myvariable[0] = new myObject("that")
myvariable[1] = new myObject("this")function myObject(name)
this.name =name<end psuedo script>
<start psuedo script src='my/path/to/javascript.js>
<end psuedo script>

What you are doing should work - I just tested it and it worked for me. You must just have an errant character somewhere or something. This is what I did.
The main page has the code:
**************************
<html><head>
<script>
var theMsg = 'It Works!';
</script>
<script language=javascript src=temp.js></script>
</head><body>
Start
<script>showMsg ();</script>
End
</body>
</html>
**************************The js page has the code:
**************************
function showMsg () {
alert(theMsg);
}
**************************The message was correctly displayed. As long as the variables are declared outside of any function, they should be global.
Michael J

I was writing my response before I saw your second post. The only thing I can think of is that perhaps objects are not global. Have you tried the code out with all of it written to the single document?
Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |