Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Seperate common functions across directories

    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?

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Location
    Edinburgh
    Posts
    1,352
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ]|V|[agnus
    Does JS have anything like @import?
    er ... surely you know this already:

    PHP Code:
    <script type="text/javascript" src="/rootdir/anotherdir/file.js"></script> 
    *keep it simple (TM)

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    New Coder
    Join Date
    Dec 2003
    Location
    At work...still
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My first idea is
    <SCRIPT language="JavaScript" src="/js/myjavascript.js"></SCRIPT>

    but I'm not sure what you mean in your 2nd post
    Last edited by WillGibson; 01-27-2005 at 07:02 PM.
    Why?
    Ok, but Why?

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    if(typeof extraFunction == 'function') { extraFunction(); }
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #6
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I must have had something setup improperly because I thought I tried something like this:

    Code:
    <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:

    Code:
    function init() {
    functionFromCommon();
    functionFromBehaviors();
    }
    window.onload = init;
    only the functions from _common.js were not available as I had expected. I will try again.
    Last edited by ]|V|[agnus; 01-27-2005 at 07:30 PM.

  • #7
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure enough, user error is the culprit. Thanks everybody.

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #9
    Regular Coder
    Join Date
    May 2004
    Location
    Minneapolis, MN, USA
    Posts
    904
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •