/    Sign up×
Community /Pin to ProfileBookmark

Can an external js file be included from within another external js file?

Is there a way to specify that an external js file get included whenever another external js file is loaded? For example, if I have an external file myFile.js, is there some directive, line of code, etc. that I can add to the top of the page that will allow me to include the file anotherFile.js? I don’t know if this is possible or not, but I am thinking it might be something like importing other .css files from within an external css file.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@PittimannJun 15.2004 — Hi!

The problem is: js files must not include tags. What you can do is: prepare a tag to an external js file with an id and a blank src.

When the code in your first js file reached the point, you want to include the second file, just assign the appropriate src to the "blank" script tag referencing to it by its' id.

Cheers - Pit
Copy linkTweet thisAlerts:
@VladdyJun 15.2004 — <i>
</i>function loadJS(filename)
{ scriptNode = document.createElement('script');
scriptNode.src = filename;
scriptNode.type = 'text/javascript';
(document.getElementsByTagName('head'))[0].appendChild(scriptNode);
}
×

Success!

Help @wood_tah 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.3,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...