passing var from <head> to <body>

Discussion in 'Javascript' started by Frances, Sep 19, 2005.

  1. Frances

    Frances Guest

    <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..
    Frances, Sep 19, 2005
    #1
    1. Advertising

  2. Frances

    Frances Guest

    Zoe Brown wrote:
    > "Frances" <> wrote in message
    > news:432edd80$0$246$...
    >
    >><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...
    Frances, Sep 19, 2005
    #2
    1. Advertising

  3. Frances

    Lee Guest

    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.
    Lee, Sep 19, 2005
    #3
  4. Frances

    Mick White Guest

    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]
    Mick White, Sep 19, 2005
    #4
  5. Lee <> 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.
    Christopher Benson-Manica, Sep 19, 2005
    #5
  6. Frances

    Frances Guest

    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>
    Frances, Sep 19, 2005
    #6
  7. Frances

    Lee Guest

    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.
    Lee, Sep 19, 2005
    #7
  8. Frances

    Lee Guest

    Christopher Benson-Manica said:
    >
    >Lee <> 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.
    Lee, Sep 19, 2005
    #8
  9. Frances

    Frances Guest

    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......
    Frances, Sep 19, 2005
    #9
  10. Frances

    Randy Webb Guest

    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
    Randy Webb, Sep 19, 2005
    #10
  11. Lee <> wrote:

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


    <html><script>
    var foo;
    </script></html>

    What is the scope of foo?


    --
    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.
    Christopher Benson-Manica, Sep 19, 2005
    #11
  12. Frances

    Lee Guest

    Christopher Benson-Manica said:
    >
    >Lee <> wrote:
    >
    >> That would be nice, if there was a way to do so in Javascript.

    >
    ><html><script>
    > var foo;
    ></script></html>
    >
    >What is the scope of foo?


    It's quite a stretch to call that "declaring it as global".
    There is nothing about the statement that makes it global,
    simply the fact that the statement appears in a global scope.

    In production code, I find it best to simply name globals
    appropriately, regardless of where they are first used:

    function popup() {
    globalPopupHTML="<html><body><p>Hello, world!</p></body></html>";
    ...
    }
    Lee, Sep 19, 2005
    #12
  13. Lee <> wrote:

    > It's quite a stretch to call that "declaring it as global".
    > There is nothing about the statement that makes it global,
    > simply the fact that the statement appears in a global scope.


    That's all I really intended to imply. Maybe

    window['foo']='foo';

    ?

    > In production code, I find it best to simply name globals
    > appropriately, regardless of where they are first used:


    > function popup() {
    > globalPopupHTML="<html><body><p>Hello, world!</p></body></html>";
    > ...
    > }


    I'd probably name it gPopupHTML, if I were to take that route.

    --
    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.
    Christopher Benson-Manica, Sep 19, 2005
    #13
  14. Frances

    web.dev Guest

    Frances wrote:
    > <html>
    > <head>
    > <script>

    Don't forget to include the type attribute:

    <script type = "text/javascript">

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


    Have the selItem as a global variable:

    var selItem = "";

    function doIt()
    {
    ...code statements...
    selItem = list.option[list.selectedIndex].value;
    ...code statements...
    }

    Don't forget to have a closing script tag:

    </script>

    > </head>
    >
    >
    > <body>
    > <script>


    <script type = "text/javascript">

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


    Now you can use the selItem variable here since it was declared global.

    > </script>
    > </body>
    >
    > this is for dynamic content.. what prints depens on what item user
    > selects in sel obj.. thank you..
    web.dev, Sep 20, 2005
    #14
  15. Frances

    Randy Webb Guest

    web.dev said the following on 9/19/2005 7:31 PM:
    > Frances wrote:
    >
    >><html>
    >><head>
    >><script>

    >
    > Don't forget to include the type attribute:
    >
    > <script type = "text/javascript">
    >
    >>function doIt() {
    >> var list = document.forms[0].product;
    >> var selItem = list.options[list.selectedIndex].value;
    >> ^^^^^^^

    >
    >
    > Have the selItem as a global variable:
    >
    > var selItem = "";
    >
    > function doIt()
    > {
    > ...code statements...
    > selItem = list.option[list.selectedIndex].value;
    > ...code statements...
    > }
    >
    > Don't forget to have a closing script tag:
    >
    > </script>
    >
    >> </head>
    >>
    >>
    >> <body>
    >><script>

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

    >
    >
    > Now you can use the selItem variable here since it was declared global.


    But it will blank, as you defined it as "". The desired (I am assuming)
    effect is to have the list.option[list.selectedIndex].value placed where
    they have the document.write statement.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Randy Webb, Sep 20, 2005
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian W
    Replies:
    10
    Views:
    768
    Brian W
    Jul 2, 2003
  2. Big D
    Replies:
    2
    Views:
    338
    Jeffrey Tan[MSFT]
    Jan 6, 2004
  3. showme

    PHP and ASP.NET go HEAD to HEAD

    showme, Jul 8, 2004, in forum: ASP .Net
    Replies:
    13
    Views:
    725
    Sherif ElMetainy
    Jul 10, 2004
  4. Karl
    Replies:
    0
    Views:
    395
  5. Jiho Han

    Write into <HEAD></HEAD> section?

    Jiho Han, Jan 14, 2004, in forum: ASP .Net Building Controls
    Replies:
    6
    Views:
    235
    Sam Fields
    Jan 16, 2004
Loading...

Share This Page