+ Reply to Thread
Results 1 to 8 of 8

Thread: Including JavaScript In Another File

  1. #1
    Junior Member
    Join Date
    Jun 2006
    Posts
    46

    Default Including JavaScript In Another File

    Is it possible to include one JavaScript file in another JavaScript file like you can do with the php include() function? If this isn't possible, can/how can you have multiple onclick functions in one statement?
    Last edited by phpnut; 11-11-2006 at 09:52 AM.
    My ongoing projects...
    www.naturesmagazine.com
    www.energyreform.org *new domain*
    www.photographyavenue.com
    ----
    You may also remember me as imnewtophp...

  2. #2
    Senior Member
    Join Date
    Jun 2006
    Location
    Gateshead
    Posts
    224

    Default Re: Including JavaScript In Another File

    I don't know if it's possible to place a file within a file in JS but if it isn't I don't think it's possible to have more than one onclick function as with the nature of that kind of programming one action produces one result.

    Just thinking maybe theres a way of including many actions in one statement, rather than trying:-

    onclick (doaction1)
    onclick (doaction2)

    try:-

    onclick (doaction1, doaction2)

  3. #3
    Forum Leader
    Join Date
    Jul 2005
    Location
    Leeds, England
    Posts
    648

    Smile Re: Including JavaScript In Another File

    I'm not sure if JavaScript has it's own direct include function, but it's possible to hook another JS file into a page dynamically using the DOM (good) or document.write() (not so good).
    This would basically entail creating another <script> include by editing the page with one JavaScript file so that the second is included when needed.
    http://www.phpied.com/javascript-include/ has a basic run down of how to do this with the DOM and other methods, but since the DOM is preferred, I'll go over that in more detail.
    Code:
    Code:
    function include_dom(script_filename) {
        var html_doc = document.getElementsByTagName('head').item(0);
        var js = document.createElement('script');
        js.setAttribute('language', 'javascript');
        js.setAttribute('type', 'text/javascript');
        js.setAttribute('src', script_filename);
        html_doc.appendChild(js);
        return false;
    }
    First line: Set up a variable that'll direct the script to where it needs to write the new include (in the head section) by the tag name, which is "head".
    Line 2: Create a new script tag element as a variable so that you can edit it before outputting it to the page.
    Lines 3 & 4: Set attributes for the element (the first part is the attibute and the second part is the value).
    Line 5: Same as above, but this is where the file you're including comes in by making use of the function attribute.
    Line 6: Basically, this just goes ahead and adds the new element so that we can use it.
    Line 7: Returns 0, meaning not true.

    If you need to execute multiple javascript functions at once via onclick, you can do this by separating them with a semicolon such as:
    HTML Code:
    <a href="#" onclick="functionone('params'); functiontwo('params')">This</a>
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!

  4. #4
    Junior Member
    Join Date
    Jun 2006
    Posts
    46

    Default Re: Including JavaScript In Another File

    Thanks guys! I have one more question though...
    I wanted to know how to do this so when a user clicked a link, my AJAX would get the necessary information and then display it through a scriptaculous effect. I tried doing this with two onclick elements, but the user would need to click the link twice for each function to occur. Is there any way I could do this?
    My ongoing projects...
    www.naturesmagazine.com
    www.energyreform.org *new domain*
    www.photographyavenue.com
    ----
    You may also remember me as imnewtophp...

  5. #5
    Forum Leader
    Join Date
    Jul 2005
    Location
    Leeds, England
    Posts
    648

    Smile Re: Including JavaScript In Another File

    HTML Code:
    <a href="#" onclick="new Ajax.Updater('element', '/myscript.php?param=value', {asynchronous:true, evalScripts:true, onComplete:function(request){new Effect.BlindDown('element',{duration:0.5});}}); return false;">This is a link</a>
    That code uses Prototype and scriptaculous to fetch the results of a script and to show it using a scriptaculous effect.
    Change 'element' to what you want to change (the I.D.), the script to your script that'll pass on the results, the effect to whatever scriptaculous effect you want (there's a lot of them) and duration to however long you feel (in seconds, so the above code is 0.5 seconds).
    This doesn't toggle like the other examples I've posted before and yet again, this was written by Rails using it's inbuilt AJAX helpers, not directly by me.
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!

  6. #6
    Advanced User WatchOut's Avatar
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    362

    Default Re: Including JavaScript In Another File

    Solved it phpnut? Hmm, this is all new to me hehe.
    Computer Forumz

    www.computerforumz.com

  7. #7
    Junior Member
    Join Date
    Jun 2006
    Posts
    46

    Default Re: Including JavaScript In Another File

    Thanks, Nuvo. I'm a beginner @ JavaScript, and thanks to you I've been able to create some pretty sweet Ajax apps. If you want, you can check em out at http://naturesmagazine.com and sign up for a membership. There, you can see the article management system i've built with ajax/scriptalous.
    My ongoing projects...
    www.naturesmagazine.com
    www.energyreform.org *new domain*
    www.photographyavenue.com
    ----
    You may also remember me as imnewtophp...

  8. #8
    Forum Leader
    Join Date
    Jul 2005
    Location
    Leeds, England
    Posts
    648

    Smile Re: Including JavaScript In Another File

    Actually, I don't really know JavaScript myself.
    I've done some very small things with it such as a bit of code for a small editor toolbar which forms part of my CMS (admitedly, some of the more complex stuff was borrowed and modified from a free forum script, but this will change).
    The thing is that due to being able to program, I can look at what Rails puts out and think "right, so this does this and that does that", meaning I can explain how to edit it to some degree.
    All of my AJAX code started with Rails and although you have to put code into Ruby on Rails to get AJAX out, the Ruby you put in is pretty readable.
    I really should pick up PHP or Delphi coding again, but I'm busy making Rails my play thing.
    I might also take up Django, though I have much less experience with that than with Rails or PHP, and Delphi or Object Pascal as it's more widely know,n, isn't really a web language.
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Debugging "C" And "C++" Programs Using "gdb"
    By lionsgate in forum Tutorials
    Replies: 2
    Last Post: 09-22-2010, 06:58 AM
  2. Make Copy Protected CD (Tutorial Contest)
    By chirag in forum Tutorials
    Replies: 0
    Last Post: 11-26-2006, 11:30 AM
  3. Replies: 0
    Last Post: 11-26-2006, 12:53 AM
  4. Windows Shortcuts (Tutorial Contest)
    By WatchOut in forum Tutorials
    Replies: 0
    Last Post: 11-17-2006, 11:15 AM
  5. Including remote file security
    By shield in forum PHP
    Replies: 3
    Last Post: 10-27-2005, 10:30 PM

Posting Permissions

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