www.webdeveloper.com
Results 1 to 4 of 4

Thread: JavaScript variables and src includes

  1. #1
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    3

    JavaScript variables and src includes

    Hi all,
    I am using both inbedded script as well as external script in a page. The problem i face is that global variables declared in the inbedded script seem to lose their values as soon as i include the external script using the <script src=something> tag.
    Is there any way that i can keep the values in the global variables and use the external scripts?
    Any help or suggestions are welcome, thank you.

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473

    Seeing double?

    Apparently, you have duplicated the names of some variables that are in the include script in the embedded script. Javascript (and browsers) are linear parsing interpreters. That means that things are built as they are read, in source file order. If your include script is just instantiating global variable values, you can place include after the embedded script. Either that, or fix the duplicate names.

  3. #3
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    3

    No doubles

    Thanks for the reply,
    I'm not declaring anything twice. The page contains a JavaScript menu (2 external js files), an embedded JavaScript variable (aKpi = new Array()) and to embedded JavaScript functions (setKpis() and getKpis(frm)).
    Function setKpis is called when the page is loaded and filles the aKpi array. Function getKpis is called from a select (options drop-down list) to populate a second select (options drop-down list).
    The Body Tag:
    <body ... onload="setKpis()">
    The Select Tag:
    <select ... onchange="getKpis(this.form)">
    The Script Tags:
    <script src="SomeName" type="text/javascript"></script>
    <script src="SomeName" type="text/javascript"></script>
    <script> aKpi declaration and embedded functions </script>
    <noscript>Your browser does not support script</noscript>

    This works fine if i remove the <script src...> tags, but the minute i put them in the array seems to lose its contents (well, the second select list comes out blank when i select from the first). Maybe i'm doing something wrong? i've also tried to declare the array in the header tag.

  4. #4
    Join Date
    Nov 2002
    Location
    South Africa
    Posts
    3

    More Information

    After posting the above reply I've found that in fact when I include the <script src="SomeFile">, the page no longer runs the onload event of the <body> tag. This is why I have no values in the aKpi array.
    I found that the imported JS overrides the onload event of the <body> tag, so i included a call to the embedded function in the imported JS and now everything works.
    Thank you for the help.
    Last edited by sirpaddi; 11-29-2002 at 03:15 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles