What is the equivalent of #include filename.hpp

Discussion in 'Javascript' started by ArbolOne, Jan 12, 2009.

  1. ArbolOne

    ArbolOne Guest

    Hey boys and girls!
    In my javascript program I would like to implement a third party
    application, how do I go about including (#include filename.hpp)
    another file in my javascript file?

    Thanks in advance
     
    ArbolOne, Jan 12, 2009
    #1
    1. Advertisements

  2. ArbolOne wrote:
    > In my javascript program I would like to implement a third party
    > application, how do I go about including (#include filename.hpp)
    > another file in my javascript file?


    As often, so here, you would need to define what you mean with "javascript
    program". Which ECMAScript implementation are we talking about here, and in
    which environment does it run, to begin with?

    As a rule of thumb, you should not try to emulate includes with scripting as
    that is inherently unreliable (there is no specification as to how user
    agents should handle that, for example). Instead, you should simply add
    another `script' element, server-side dynamically or client-side statically,
    to your (X)HTML document.

    But YMMV, depending on what you consider a "javascript program" and
    "javascript file" here.

    <http://jibbering.com/faq/#posting>


    PointedEars
     
    Thomas 'PointedEars' Lahn, Jan 12, 2009
    #2
    1. Advertisements

  3. ArbolOne

    Jorge Guest

    On Jan 12, 11:36 pm, ArbolOne <> wrote:
    >
    > In my javascript program I would like to implement a third party
    > application, how do I go about including (#include filename.hpp)
    > another file in my javascript file?
    >


    Forget it, there's no way to #include a file in a (.js) file. This
    isn't C !

    --
    Jorge.
     
    Jorge, Jan 12, 2009
    #3
  4. ArbolOne

    ArbolOne Guest

    Thanks folks!
    I got it!
     
    ArbolOne, Jan 13, 2009
    #4
  5. ArbolOne

    Ralf Beutler Guest

    Hi,

    Jorge wrote:
    >
    > Forget it, there's no way to #include a file in a (.js) file. This
    > isn't C !


    I beg to discuss following script I found:
    (please stick to technology not coding conventions)

    // reader stuff
    ////////////////////////////////////////////////////////////////////////////

    // global: list of javascript files to include
    g_includeList = [];

    // global: flag rembering document.onload was already triggered
    g_onLoadTriggered = false;

    /**
    * include a javascript file (like C++, PHP or Actionscript
    */
    function include( srcfile ) {

    if( !is_string(srcfile) ){
    return;
    }
    g_includeList.push( srcfile );

    if( g_includeList.length == 1 && !g_onLoadTriggered ){
    registerNodeListener(window, "load", _scriptIncluder );
    } else {
    _scriptIncluder();
    }
    }


    /**
    * private function for include()
    *
    * Asures loading of javascript files even if document.onload already
    * was triggered
    */
    function _scriptIncluder(e) {
    // gets executed, when onload triggers for window
    if( defined(e) || g_onLoadTriggered ) {
    g_onLoadTriggered = true;
    var str;
    var cacheStop = '?' + Math.random()*1000;
    while( str=g_includeList.pop() ) {
    str += cacheStop;
    // this getText function makes this AJAX Voodoo
    var code = getText(str+cacheStop, null, true);
    if( code == null ){ return 0; }
    try {
    var value = eval( code );
    }
    catch(e) {
    alert( 'core::include : Exception while executing "eval" on ' +
    file content.\n\nFile:\n'+str+'\n\nError:\n'+e.message );
    return 0;
    }
    }
    }
    // as above is recursive, include _main.js which lastly invokes main()
    // as program start
    if( defined(e) ) {
    include('src/_main.js');
    }
    return 1;
    }

    /////////////////////////////////////////////

    Usage:
    <!-- load the above script with added AJAX stuff to load the files -->
    <script type="text/javascript" src="src/core_functions.js"></script>

    <script type="text/javascript">
    include('startup.js');
    </script>

    /////////////////////////////////////////////

    startup.js can contain as many includes as you like and this works
    recursively

    // include everything
    include('src/logging/log.screen.js');

    include('src/screenbuilder/screenbuilder.js');
    include('src/gui_elements/buttons.js');
    include('src/message/msgmanager.js');
    include('src/message/messages.js');

    // further code

    /////////////////////////////////////////////

    In addition I like to point out that I would like to prefer a server
    side #include mechanism.

    br | rb
    --
    please excuse my french
     
    Ralf Beutler, Jan 14, 2009
    #5
  6. In comp.lang.javascript message <4b3224c2-c5a4-453f-a7dc-6fd2dfc9c177@40
    g2000prx.googlegroups.com>, Mon, 12 Jan 2009 15:05:38, Jorge
    <> posted:
    >On Jan 12, 11:36 pm, ArbolOne <> wrote:
    >>
    >> In my javascript program I would like to implement a third party
    >> application, how do I go about including (#include filename.hpp)
    >> another file in my javascript file?

    >
    >Forget it, there's no way to #include a file in a (.js) file. This
    >isn't C !


    That effect can be obtained if one does not insist on the exact grammar
    "#include".

    See today's <URL:http://www.merlyn.demon.co.uk/$1.htm>, which includes
    in the normal manner my $1.js, which contains just
    document.write(
    '<script type="text/javascript" src="include2.js"></script>')
    and include2.js contains
    var TestCounter = 1

    Page $1.htm currently contains
    alert(TestCounter)
    which executes successfully in Firefox 3.0.5, IE7, Opera 9.27, Safari
    3.2.1,

    However, your answer appears to be an inexact match to the OP's
    question.

    --
    (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
    Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
     
    Dr J R Stockton, Jan 14, 2009
    #6
  7. ArbolOne

    Jorge Guest

    On Jan 14, 5:46 pm, Dr J R Stockton <> wrote:
    > In comp.lang.javascript message <4b3224c2-c5a4-453f-a7dc-6fd2dfc9c177@40
    > g2000prx.googlegroups.com>, Mon, 12 Jan 2009 15:05:38, Jorge
    > <> posted:
    >
    > >On Jan 12, 11:36 pm, ArbolOne <> wrote:

    >
    > >> In my javascript program I would like to implement a third party
    > >> application, how do I go about including (#include filename.hpp)
    > >> another file in my javascript file?

    >
    > >Forget it, there's no way to #include a file in a (.js) file. This
    > >isn't C !

    >
    > That effect can be obtained if one does not insist on the exact grammar
    > "#include".
    >
    > See today's <URL:http://www.merlyn.demon.co.uk/$1.htm>, which includes
    > in the normal manner my $1.js, which contains just
    >   document.write(
    >     '<script type="text/javascript" src="include2.js"></script>')
    > and include2.js contains
    >   var TestCounter = 1
    >
    > Page $1.htm currently contains
    >   alert(TestCounter)
    > which executes successfully in Firefox 3.0.5, IE7, Opera 9.27, Safari
    > 3.2.1,


    What you're doing is inserting (with d.write()) a script in the body
    (to be executed later on when (the body gets) parsed, I guess) from
    within a previous script... I think I get it, but what a mess... :)

    > However, your answer appears to be an inexact match to the OP's
    > question.
    >


    I thought he wanted to insert text in the middle of a .js file, such
    as when you #include stdio.h, into a .c source, for example...

    Regards,
    --
    Jorge.
     
    Jorge, Jan 14, 2009
    #7
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,239
    Travis Murray
    Aug 24, 2004
  2. Steven T. Hatton

    #include <boost/intrusive_ptr.hpp>

    Steven T. Hatton, Jun 15, 2005, in forum: C++
    Replies:
    4
    Views:
    2,835
    Steven T. Hatton
    Jun 15, 2005
  3. Replies:
    1
    Views:
    2,014
    Roland de Ruiter
    Jun 15, 2006
  4. Ed
    Replies:
    10
    Views:
    51,144
    alok000707
    Jul 13, 2010
  5. Tomás

    Gems -- #include <gems.hpp>

    Tomás, Mar 4, 2006, in forum: C++
    Replies:
    7
    Views:
    650
    Tomás
    Mar 5, 2006
  6. Colin Caughie
    Replies:
    1
    Views:
    982
    Shooting
    Aug 29, 2006
  7. Andreas Bogenberger
    Replies:
    3
    Views:
    1,292
    Andreas Bogenberger
    Feb 22, 2008
  8. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    1,021
    Jonathan N. Little
    May 30, 2008
Loading...