/    Sign up×
Community /Pin to ProfileBookmark

calling another js function from an external js

Hi, is it possible to include js files in an external js?

For example, in “external.js,” can I call a function from “scriptA.js” like this?

document.write(“<script src=’scriptA.js’ language=’JavaScript’></script>”);
document.write(“<script src=’scriptB.js’ language=’JavaScript’></script>”);

functionFromScriptA();

The javascript console in Firefox prints out “Error: functionFromScriptA is not defined”

Are there any ways to get around this? I really want my js logic separated from my html page, preferably in one file.

Thanks!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@SelrachFeb 27.2006 — Sure you can, make sure that the src is pointing to the right location....I've done it with the following:
[CODE]document.write("<SCRIPT LANGUAGE='JavaScript' SRC='/generic.js' TYPE='text/javascript'></SCRIPT>");
[/CODE]
Copy linkTweet thisAlerts:
@jamaycaauthorFeb 27.2006 — thanks for the reply...but that doesn't work for me either. I think it's because scriptA doesn't really exist as anything but a string while external.js is still being processed?

(I can't see why else functionFromScriptA would be undefined...)

Anyone know any work-arounds?

thanks
Copy linkTweet thisAlerts:
@Orc_ScorcherFeb 27.2006 — document.write(
"&lt;script src='scriptA.js'&gt;&lt;/script&gt;",
"&lt;script src='scriptB.js'&gt;&lt;/script&gt;",
"&lt;script&gt;functionFromScriptA();&lt;/script&gt;"
);
Copy linkTweet thisAlerts:
@SelrachFeb 27.2006 — Actually its more than likely that the src in the include is pointing to the wrong location and it can't find anything to include. Make sure your links are correct. When you do document.write() it basically automatically puts that in there...so if you include script A in script B at the top of the js file, Script B can use anything that script A has in it. I do this extensively in some of my libraries for work
Copy linkTweet thisAlerts:
@jamaycaauthorMar 03.2006 — actually, my src was pointing to the right place...and orc scorcher's suggestion worked! thanks for your help!
×

Success!

Help @jamayca 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.21,
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,
)...