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.
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.
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.
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.
Bookmarks