Including JavaScript In Another File

phpnutphpnut Junior MemberShared Hoster
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?
My ongoing projects...
www.naturesmagazine.com
www.energyreform.org *new domain*
www.photographyavenue.com
----
You may also remember me as imnewtophp...

Comments

  • LurkinBackLurkinBack Senior Member The Royal RAM
    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) :confused:
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    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:
    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]
    <a href="#" onclick="functionone('params'); functiontwo('params')">This</a>[/html]
    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!!!
  • phpnutphpnut Junior Member Shared Hoster
    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...
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    [html]<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>[/html]
    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!!!
  • WatchOutWatchOut Advanced User VPS - Virtual Prince of the Server
    Solved it phpnut? Hmm, this is all new to me hehe.
    Computer Forumz

    www.computerforumz.com
  • phpnutphpnut Junior Member Shared Hoster
    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...
  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    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!!!
Sign In or Register to comment.