/    Sign up×
Community /Pin to ProfileBookmark

Can I write a script that loads other scripts?

Instead of using many external scripts and CSS files in the header, can I just load them all using Javascript? The easiest way that I can think of is using document.write(‘<link … /><script…></script>’) to get all the external files.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jul 19.2010 — I definitely wouldn't load CSS with JavaScript. That excludes people who don't have JavaScript enabled, but may be able to use CSS. (The percentages are irrelevant in case anyone intends on telling me that this is negligible, for such a simple matter, excluding anyone because they don't have JavaScript, even one user, is ridiculous).

How many have you? Most handwritten sites would have one to four CSS files, and maybe two or three JavaScript at most, normally only one. If you have more than that, I wonder would you need to combine some of them.

But if you want to save writing it again and again on every page, you are looking for a server-side include. As I recall you know some PHP, so look at [url=http://php.net/manual/en/function.include.php]include()[/url].

It is possible with JavaScript, but highly inadvisable.
Copy linkTweet thisAlerts:
@cbVisionJul 19.2010 — You can import multiple stylesheets by using the following line in your CSS file. This would limit your CSS to one call in the HTML.

[CODE]@import url("import1.css");[/CODE]

Here's a tutorial on how you can include JavaScript inside other JavaScript files:

http://www.cryer.co.uk/resources/javascript/script17_include_js_from_js.htm
Copy linkTweet thisAlerts:
@narutodude000authorJul 19.2010 — I do use PHP. I only have one PHP file for the header which is included in the other files. However, I like the HTML to be as clean as possible.

I am using phpBB. But phpBB has some javascript that is written directly into the header, instead of using an external file. Does anyone know why?
Copy linkTweet thisAlerts:
@rnd_meJul 20.2010 — you can load all the css with one stylesheet as shown.

you can also create a dispatching external javascript that loads the other files you need.

this isn't the most eloquent approach, but it's short and it works:
[CODE]
function addScript(u){ var sc2=document.createElement('script'); sc2.src=u; return document.getElementsByTagName('*')[1].appendChild(sc2); }


//add some external scripts (syntax demo):
addScript("http://mysite.com/path/file.js");
addScript("/path/file2.js");
addScript("file3.js");


[/CODE]
Copy linkTweet thisAlerts:
@narutodude000authorJul 20.2010 — Thanks! So now all I'll need is one CSS and one Javascript on the header.
×

Success!

Help @narutodude000 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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