/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Question: Can you delay load of an external JS file?

I would like to retrieve the contents of different external JS files with a textbox or drop-down selection.
Is this possible?

[code]
<select id=”SBox” onChange=”LoadJSinfo(this.value)”>
<option value=””>Choose Month</option>
<option value=”Jan.js”>Jan</option>
// …
<option value=”Dec.js”>Dec<option>
</select>
[/code]

Where ‘Jan.js’ … ‘Dec.js’ would be stand alone external files.

I am stuck on how to create the function ‘LoadJSinfo(filename)’.
My initial thoughts were:

[code]
function LoadJSinfo(filename) {
<script type=”text/javascript” src=filename></script>
}
[/code]

But obviously, that doesn’t work.

Is there a way to load the external file contents after the page has been displayed?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangMay 28.2009 — Use DOM methods:function addScript(JSfileName) {
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.src = JSfileName;
document.body.appendChild(js);
}
Copy linkTweet thisAlerts:
@JMRKERauthorMay 28.2009 — Thank you 'Fang'. I'll give it a try.

Appreciate the information. ?
Copy linkTweet thisAlerts:
@rnd_meMay 28.2009 — Use DOM methods:function addScript(JSfileName) {
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.src = JSfileName;
document.body.appendChild(js);
}
[/QUOTE]


shouldntt the script tag go in the head instead of the body?

I thought some browsers wouldn't fire that...

good news if i'm wrong, that's shorter code.
Copy linkTweet thisAlerts:
@felgallMay 28.2009 — The usual place to put JavaScript tags these days is at the bottom of the body. That way any content that needs to run as soon as possible and which needs to interact with the page can run straight away because the HTML has already loaded before the JavaScript. Also since JavaScript files single threads for download if hard coded in the HTML placing them at the bottom of the body doesn't hold up the download of anything else in the page. For script tags added dynamically from JavaScript it doesn't make any difference whether you add them to the end of the head or end of the body since in either case the page is already downloaded before the JavaScript.
×

Success!

Help @JMRKER 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 4.29,
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,
)...