/    Sign up×
Community /Pin to ProfileBookmark

how to include an external JS file within a JavaScript code?

Hi people

I am running a JavaScript code that needs to include an external JS file if some condition evaluates to true.

I need something like:

[code]
<script language=”JavaScript” type=”text/javascript”>
var condition = ..
if (condition == true) {
<script language=”JavaScript” src=”http://www.mysite.com/file.js”></script>
}
</script>
[/code]

Obviously this doesn’t work.
Does anyone know how this can be done in JavaScript?

thanks in advance
Jason

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@TinypantsApr 17.2007 — <i>
</i>var e = document.createElement('script');
e.src = 'somefile.js';
e.type = "text/javascript";
document.getElementsByTagName('head')[0].appendChild(e);


This will load js file to the header. You will need to use another method to determine when the file is loaded as you will not be able to access any function within the file tell its done.

One method is putting the function/command you are going to be running next at the end of .js file. That way when the file finishes loading it will run the command.
Copy linkTweet thisAlerts:
@svidgenApr 17.2007 — Hello,

Perhaps you could try something simliar to the following (which I tested in FF, IE6, and Safari):

[code=html]
<html>
<head>

<script>
function bodyLoaded() {
new_script = document.createElement('script');
new_script.setAttribute("language", "JavaScript");
new_script.setAttribute("src", "ajax-alert.js");
document.body.appendChild(new_script);
} // bodyLoaded()
</script>

</head>
<body onLoad="bodyLoaded();">

this is my document.

</body>
</html>[/code]


My test ajax-alert.js file is as follows:

[CODE]alert("something");[/CODE]

Basically, create a script element, set the language and src attributes, and append it into the body. JavaScript in an HTML setting is built to be able to do exactly what you need. This is a rather elegant method of dealing with AJAX requests in general. However, I think [B]any[/B] solution from the AJAX family is really what you're looking for.

If you think this method is ugly, look into using the XMLHttpRequest method or using an invisible iframe for requests. The invisible iframe method is also (as I recall) compatible cross-browser, which makes it favorable for some.

In brief, create an iframe. Set it's visiblity to hidden. Use it to load URL's. Execute the returned value. I'm not sure the code below is exactly what you'd need, but it should be close:

[CODE]document.getElementById('iframeid').location=[I]newURL[/I];
eval(document.getElementById('iframeid').innerHTML);
[/CODE]


IE may still make those annoying (to some) clicking noises when an iframe loads a URL, which is why some may steer away from that method. There are a lot of options out there. You just have to pick one.

Hope that helps.
×

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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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