SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2011
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    When using objects... (easy question)

    Say I have two javascript external sources. Is it possible for me to call one's functions from the other? Do I need to use JS's form of include within the other or can i just say "one.function()"?

  2. #2
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    functions are 'global' in scope, so as long as the function has been loaded you can call it from anywhere. The trick is you have to load them in the order you run them....

    So for example, if you had in "test.js" a "function test()", and in "doit.js" tried to call test(), if you included test.js in your HTML before doit.js, you reverse them, it would fail...

    Unless of course the routine that calls test() is not called until after both scripts have been loaded into memory.

    That's the key -- javascript assigns it's namespace while loading, but doesn't resolve dependency until you actually RUN the scripts... if your calls are all nice and neatly wrapped in functions and you initiate them manually -- or you wait for "onload" before running any scripting -- you can cross-call between internal scripting and multiple external scripts until you're blue in the face.

    Basically, it's not an easy question to answer as it all hinges on the ORDER you do things in.


Bookmarks

Posting Permissions

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