Computing.Net > Forums > Web Development > javascript not recognizing variable

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

javascript not recognizing variable

Reply to Message Icon

Name: drsmartman
Date: April 7, 2005 at 19:39:13 Pacific
OS: win
CPU/Ram: n/a
Comment:

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.



Sponsored Link
Ads by Google

Response Number 1
Name: drsmartman
Date: April 7, 2005 at 21:41:11 Pacific
Reply:

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>


0

Response Number 2
Name: Michael J (by mjdamato)
Date: April 7, 2005 at 21:42:03 Pacific
Reply:

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


0

Response Number 3
Name: Michael J (by mjdamato)
Date: April 8, 2005 at 01:32:06 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


JavaScript - Ping Compute... export data to Excel file...



Post Locked

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


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: javascript not recognizing variable

IE will not recognize webpage update www.computing.net/answers/webdevel/ie-will-not-recognize-webpage-update/4072.html

very very simple HTML/PHP problem www.computing.net/answers/webdevel/very-very-simple-htmlphp-problem/783.html

Apache URL variable passing problem www.computing.net/answers/webdevel/apache-url-variable-passing-problem/150.html