/    Sign up×
Community /Pin to ProfileBookmark

including a JS file from another JS file

Hi people

this is a simple question regarding one JS file referring another JS file.
I have a JS file called a.js
every page in my application is including a.js

Now, I have a new JS file called b.js
I have code in a.js that needs to refer to code in b.js
I don’t want to modify the entire application and to include b.js everywhere a.js is included, but a.js needs to be aware of b.js somehow.

Is there a way to include b.js from a.js?
I don’t want to take the content of b.js and copy it into a.js

Is there a simple solution?

thanks

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoAug 13.2006 — If you always want b.js where you hvae a.js then the best thing to do is just place the code from b.js into a.js, but since you don't want to do that, you can add the following to your a.js so that it will always call b.js:

newJS = document.createElement('script');

newJS.type='text/javascript';

newJS.src='b.js';

document.getElementsByTagName('head')[0].appendChild(newJS);

That will create a tag to call b.js anytime that a.js loads. All you have to do is place that code into a.js and call is however you want.
Copy linkTweet thisAlerts:
@Tweak4Aug 14.2006 — Forgive me if this is a stupid question, but why couldn't you just add:

document.write('<script type="text/javascript" src="b.js">');

inside of a.js? What does the first approach buy you that this ones doesn't, aside from actually adding the b.js call to the document's head, rather than to wherever the a.js call is made?
Copy linkTweet thisAlerts:
@KravvitzAug 14.2006 — document.write() doesn't work when you serve XHTML as application/xhtml+xml.
Copy linkTweet thisAlerts:
@Tweak4Aug 15.2006 — Fair enough... thanks
×

Success!

Help @jasongr 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.19,
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,
)...