Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple question--hopefully

Status
Not open for further replies.

timcarr

Programmer
Jan 10, 2001
23
0
0
ZA
I'm still fairly new to javascript, so forgive my ignorance, but I need to know the following:

I have a function in the heading of an html page that sets the value of a variable. I'd like to use this variable in the body of the page, but from what I can tell a variable set in a function is only valid in the function. Is this true or am I not doing something correctly?

Thanks,
Tim
 
you can use javascript's document.write function to write it out in the page --

<script>document.write(variable);</script>

good luck! :)
Paul Prewett
 
Thanks for the quick response. Could you show me a simple example? The reason I ask is because this is precisely what I've been trying to do to test this out and I can't get it to work...
When I don't set the variable in a function it works fine, otherwise I get nothing...
 
Is it a builtin variable or one you create. If you create it you probably need to declare it outside the function in order to be able to access it outside the function. Try putting this outside the function:
Code:
var [i]varname[/i] = &quot;&quot;;
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Yea, I think i see your problem -- you are setting it in a function, and so when the body of the page loads, the variable has not yet been set because no event has fired that function yet --

I would suggest declaring a global variable to hold the value --

<script>
var myVar;
myVar = 'blah';
</script>

------

<table width=100% border=1 onLoad=&quot;set();&quot;>
<tr>
<td>
<script>document.write(myVar)</script>
</td>
</tr>
</table>
 
OK, so from what I understand the following code should work...

Code:
<html>
<head>
<script>
var myVar;

function set() {
myVar = 'blah';
}
</script>
</head>
<body>
<table width=&quot;100%&quot; border=&quot;1&quot; onLoad=&quot;set();&quot;>
<tr>
	<td>
    	<script>document.write(myVar)</script>
	</td>
</tr>
</table>
</body>
</html>

But I keep getting undefined for the value of the variable... Am I making some obvious/stupid mistake?
 
The page is being loaded (and the document.write executed) BEFORE the onLoad event is called. The value of myVar has not yet been set when the document.write is executed.
If you change the script like this:
Code:
var myVar = &quot;gack&quot;;
The value won't be unitialized and the work &quot;gack&quot; will be displayed.
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
That's because its parsing the script tags before the onload (a useful feature) and at that point myVar is undefined.

Also, you have the onload event attached to the table. That never fires, you need to attach it to the body.

You have several options:

-declare the value of myVar at the top where you intialize it

-access the td through the dom, and call a function onlaod to set it:

<html>
<head>
<script>
var myVar;

function set() {
document.getElementById('benluc').innerHTML = myVar = '<a href=&quot; webFx :)</a>';
}
</script>
</head>
<body onLoad=&quot;set();&quot;>
<table width=&quot;100%&quot; border=&quot;1&quot; >
<tr>
<td id=&quot;benluc&quot;>
</td>
</tr>
</table>
</body>
</html>

Now a link is inside the table. This will only work in IE5/mozilla. jared@eae.net -
 
Alright, thanks for all the help guys.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top