...

View Full Version : Seperate common functions across directories



]|V|[agnus
01-27-2005, 06:33 PM
I am trying to figure out how to seperate some common functions that I want available across several levels of site into something I can include from a central source.

I tried processing my files using a SSL, using an include statement, but that doesn't seem to work. I tried multiple script tags, thinking that the functions from one are visible to the other, but I realize this isn't the case.

Does JS have anything like @import?

jbot
01-27-2005, 06:43 PM
|V|[agnus']Does JS have anything like @import?

er ... surely you know this already:



<script type="text/javascript" src="/rootdir/anotherdir/file.js"></script>

]|V|[agnus
01-27-2005, 06:51 PM
Not the same... I'm not being clear apparently.

Say I've got these common functions that I want in Directory A and Directory B... files in Directory A also need a supplementary set of functions specific to that directory.

Then, I want to initialize them using window.onload = init;

So init() must have access to functions in seperate files, but can obviously be called and referenced only once itself.

WillGibson
01-27-2005, 06:59 PM
My first idea is
<SCRIPT language="JavaScript" src="/js/myjavascript.js"></SCRIPT>

but I'm not sure what you mean in your 2nd post

brothercake
01-27-2005, 07:16 PM
Multiple includes is the way to go - what you need is a typeof discriminator in the init() function that calls the extra functions, or not, depending on whether they exist:


if(typeof extraFunction == 'function') { extraFunction(); }

]|V|[agnus
01-27-2005, 07:27 PM
I must have had something setup improperly because I thought I tried something like this:



<script type="text/javascript" src="_common.js"></script>
<script type="text/javascript" src="_behaviors.js"></script>


where _common.js simply contains a number of functions, and _behaviors.js contains yet more functions in addition to something like:



function init() {
functionFromCommon();
functionFromBehaviors();
}
window.onload = init;


only the functions from _common.js were not available as I had expected. I will try again.

]|V|[agnus
01-27-2005, 07:33 PM
Sure enough, user error is the culprit. Thanks everybody.

Willy Duitt
01-27-2005, 07:38 PM
I have a suspicion that your relative path to the common.js file is incorrect, particularly depending upon where in the site the calling document resides...

Your example: src="_common.js"
Suggests that common.js resides in the same directory as the calling document, but I would assume the document is a sub-folder and perhaps several layers deep...

Try placing your common scripts which are to be called site wide in their own sub-directory of the root and no matter where you call this scripts from return to the root and back down into the scripts sub directory...

IE: ./scripts/_common.js

Anyway, if you did not understand that - it appears to be a relative path problem...

.....Willy

]|V|[agnus
01-27-2005, 07:49 PM
The problem was that I had a function throwing an exception in _common.js, which is why the subsequently declared functions were not registering as I had expected.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum