/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisNov 28.2002 — 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.
Copy linkTweet thisAlerts:
@sirpaddiauthorNov 29.2002 — 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.
Copy linkTweet thisAlerts:
@sirpaddiauthorNov 29.2002 — 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.
×

Success!

Help @sirpaddi spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...