Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > passing var from <head> to <body>

Reply
Thread Tools

passing var from <head> to <body>

 
 
Frances
Guest
Posts: n/a
 
      09-19-2005

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass it from function to here?
</script>
</body>

this is for dynamic content.. what prints depens on what item user
selects in sel obj.. thank you..


 
Reply With Quote
 
 
 
 
Frances
Guest
Posts: n/a
 
      09-19-2005
Zoe Brown wrote:
> "Frances" <(E-Mail Removed)> wrote in message
> news:432edd80$0$246$(E-Mail Removed)...
>
>><html>
>><head>
>><script>
>>function doIt() {
>> var list = document.forms[0].product;
>> var selItem = list.options[list.selectedIndex].value;
>> ^^^^^^^
>> </head>
>>
>>
>> <body>
>><script>
>>document.writeln('<div id="' + selItem + '">');// var not being
>> ^^^^^^^^ // read here..
>> // how do I pass it from function to here?
>></script>
>> </body>
>>
>>this is for dynamic content.. what prints depens on what item user selects
>>in sel obj.. thank you..

>
>
> if the content is dynamic you will need to create a function that creates
> the text and an event to trigger it. for example the onChange event of your
> select could call a function call writeDynamicContent() which then spits out
> the text in the <div>.
>
>

yes, I'm calling doIt() (function that writes dynamic content) with
onChange Event handler in sel obj.. but still content doesn't print
where I want it to b/c var isn't being passed from function to where I
want content to print... how DO you pass a variable from inside a
function to outside it? thank you very much for yr help...


 
Reply With Quote
 
 
 
 
Lee
Guest
Posts: n/a
 
      09-19-2005
Frances said:
>
>
><html>
><head>
><script>
>function doIt() {
> var list = document.forms[0].product;
> var selItem = list.options[list.selectedIndex].value;
> ^^^^^^^
> </head>
>
>
> <body>
><script>
>document.writeln('<div id="' + selItem + '">');// var not being
> ^^^^^^^^ // read here..
> // how do I pass it from function to here?
></script>
> </body>
>
>this is for dynamic content.. what prints depens on what item user
>selects in sel obj.. thank you..


It's not really a matter of visibility between the head
and body. You've declared the variable selItem to be
local to function doIt().

Removing the "var" keyword from the declaration will make
the variable global.

 
Reply With Quote
 
Mick White
Guest
Posts: n/a
 
      09-19-2005
Frances wrote:
>
> <html>
> <head>
> <script>
> function doIt() {
> var list = document.forms[0].product;
> var selItem = list.options[list.selectedIndex].value;
> ^^^^^^^
> </head>
>



list = document.forms[0].product; // Now "list" is global.

Mick

[snip]
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      09-19-2005
Lee <(E-Mail Removed)> wrote:

> Removing the "var" keyword from the declaration will make
> the variable global.


My personal preference is to explicitly declare global variables as
such.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Frances
Guest
Posts: n/a
 
      09-19-2005
Mick White wrote:
> Frances wrote:
>
>>
>> <html>
>> <head>
>> <script>
>> function doIt() {
>> var list = document.forms[0].product;
>> var selItem = list.options[list.selectedIndex].value;
>> ^^^^^^^
>> </head>
>>

>
>
> list = document.forms[0].product; // Now "list" is global.

and where is this var declaration? if I put it outside function then
function can't read it.. if I put it inside function then it can't be
read outside function.. what am I missing here? how do you OUTSIDE
functions var's you have processed inside functions? thank you very
much.. again here is situation:

<html>
<head>
<script>
function doIt() {
var list = document.forms[0].product;
var selItem = list.options[list.selectedIndex].value;
^^^^^^^
</head>


<body>
<select name="product" onChange="doIt()"> ... </select>
[...............]

<script>
document.writeln('<div id="' + selItem + '">');// var not being
^^^^^^^^ // read here..
// how do I pass var from function to here?
</script>
</body>
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      09-19-2005
Frances said:
>
>Mick White wrote:
>> Frances wrote:
>>
>>>
>>> <html>
>>> <head>
>>> <script>
>>> function doIt() {
>>> var list = document.forms[0].product;
>>> var selItem = list.options[list.selectedIndex].value;
>>> ^^^^^^^
>>> </head>
>>>

>>
>>
>> list = document.forms[0].product; // Now "list" is global.

>and where is this var declaration? if I put it outside function then
>function can't read it..


Where did you get that idea? A var declared outside of any function
is global. You can also simply leave off the "var" keyword.

 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      09-19-2005
Christopher Benson-Manica said:
>
>Lee <(E-Mail Removed)> wrote:
>
>> Removing the "var" keyword from the declaration will make
>> the variable global.

>
>My personal preference is to explicitly declare global variables as
>such.


That would be nice, if there was a way to do so in Javascript.

 
Reply With Quote
 
Frances
Guest
Posts: n/a
 
      09-19-2005
Lee wrote:
> Frances said:
>
>>
>><html>
>><head>
>><script>
>>function doIt() {
>> var list = document.forms[0].product;
>> var selItem = list.options[list.selectedIndex].value;
>> ^^^^^^^
>> </head>
>>
>>
>> <body>
>><script>
>>document.writeln('<div id="' + selItem + '">');// var not being
>> ^^^^^^^^ // read here..
>> // how do I pass it from function to here?
>></script>
>> </body>
>>
>>this is for dynamic content.. what prints depens on what item user
>>selects in sel obj.. thank you..

>
>
> It's not really a matter of visibility between the head
> and body. You've declared the variable selItem to be
> local to function doIt().
>
> Removing the "var" keyword from the declaration will make
> the variable global.
>


oh gosh, I see, it's like java then.....

so this will make it global:

var list;
var selItem;
function doIt() {
list = document.forms[0].product;
selItem = list.options[list.selectedIndex].value;
}

thank you all very much for your responses......

 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      09-19-2005
Frances said the following on 9/19/2005 1:30 PM:

> Lee wrote:
>
>> Frances said:
>>
>>>
>>> <html>
>>> <head>
>>> <script>
>>> function doIt() {
>>> var list = document.forms[0].product;
>>> var selItem = list.options[list.selectedIndex].value;
>>> ^^^^^^^
>>> </head>
>>>
>>>
>>> <body>
>>> <script>
>>> document.writeln('<div id="' + selItem + '">');// var not being
>>> ^^^^^^^^ // read here..
>>> // how do I pass it from function to here?
>>> </script>
>>> </body>
>>>
>>> this is for dynamic content.. what prints depens on what item user
>>> selects in sel obj.. thank you..

>>
>>
>>
>> It's not really a matter of visibility between the head
>> and body. You've declared the variable selItem to be
>> local to function doIt().
>>
>> Removing the "var" keyword from the declaration will make
>> the variable global.
>>

>
> oh gosh, I see, it's like java then.....
>
> so this will make it global:
>
> var list;
> var selItem;
> function doIt() {
> list = document.forms[0].product;
> selItem = list.options[list.selectedIndex].value;
> }
>
> thank you all very much for your responses......
>


This makes it global also:

list = '';
selItem = '';

But your problem seems to be that you are trying to access a variable
that is inside a function but you are trying to access it before the
functions is executed. That means the variable has not been defined, nor
created yet, so you get the error.


--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between Session["var"] and Session("var") thomson ASP .Net 10 06-20-2005 01:02 PM
Difference between Session["var"] and Session("var") thomson ASP .Net 0 06-20-2005 10:54 AM
Threads.. Session var lost, App var ok Alvin Bruney ASP .Net 1 12-02-2003 01:56 AM
Invoking constructor : Foo(var) and Foo ins(var) Alex Vinokur C++ 4 09-24-2003 04:01 PM
does "struct_name var = { 0 }; " fill var with 0? Fred C++ 3 08-10-2003 09:44 AM



Advertisments