/    Sign up×
Community /Pin to ProfileBookmark

Multiple Scripts in one external file…

Hi,

I am tied to using a CMS that doesn’t allow me to edit within the head tags but has a single preconfigured file that I can put any Javascript.

The site I am currently building will have a couple of different instances where Javascript will be needed.

For example, it will have a script controlling a drop down menu, and a script (making use of Jquery) that controls a tootip.

So, making use of one external .js file, how do you suggest I call different scripts for different tasks, and I will also need to call the Jquery library.

I would rather avoid including any js within the body tags.

Thanks for any help

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@criterion9Aug 04.2009 — If you are not able to edit between the head tags you will need to include script tags in the body. There is no other way to do it given your restrictions.
Copy linkTweet thisAlerts:
@Logic_AliAug 04.2009 — You could try something like this which does work. You may have to create and attach more than one script element in the function, and be sure not to overwrite any existing onload handler.<i>
</i>(function()
{
var sTag = document.createElement('script');

sTag.src = 'jquery.js';

sTag.type = 'text/javascript';

window.onload = function(){ /*Initialise scripts here */ };

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

})();
Copy linkTweet thisAlerts:
@rnd_meAug 04.2009 — expanding on Ali's code, here is a reusable function:

[CODE]

(function(){

function addJS(turl, timeout) {
var xJs = document.createElement("script");
document.getElementsByTagName("head")[0].appendChild(xJs);
xJs.src = turl;
return xJs;
}


addJS("jquery.js");
addJS("jqueryui.js");
addJS("hoverfx.js");


}());

[/CODE]


you don't really need to bother with an onload event, and doing so would break jQuery's $.ready functionality...
Copy linkTweet thisAlerts:
@mr_dobalinoauthorAug 04.2009 — OK thanks for your help.

so would I put the given script into the one javascript area that I have been allowed access too and the other .js files into the same directory as that file?

cheers
Copy linkTweet thisAlerts:
@rnd_meAug 05.2009 — OK thanks for your help.

so would I put the given script into the one javascript area that I have been allowed access too and the other .js files into the same directory as that file?

cheers[/QUOTE]


yes, in the same folder when using just file names.

you can also specify full urls (http:// to .js), they don't even have to be on your site...
Copy linkTweet thisAlerts:
@mr_dobalinoauthorAug 05.2009 — Great thanks,

And do I need to call that function from within script tags in the body? Would I need to put the following? Also, does it matter where I put this in the body?

<script type="text/javascript">

<!--

onload = function ()

// -->

</script>

sorry, thanks for you patience and help, I'm fairly new to this JS game as you can tell...
×

Success!

Help @mr_dobalino 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.18,
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,
)...