Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > What is the equivalent of #include filename.hpp

Reply
Thread Tools

What is the equivalent of #include filename.hpp

 
 
ArbolOne
Guest
Posts: n/a
 
      01-12-2009
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
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-12-2009
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
 
Reply With Quote
 
 
 
 
Jorge
Guest
Posts: n/a
 
      01-12-2009
On Jan 12, 11:36*pm, ArbolOne <(E-Mail Removed)> 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.
 
Reply With Quote
 
ArbolOne
Guest
Posts: n/a
 
      01-13-2009
Thanks folks!
I got it!
 
Reply With Quote
 
Ralf Beutler
Guest
Posts: n/a
 
      01-14-2009
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
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      01-14-2009
In comp.lang.javascript message <4b3224c2-c5a4-453f-a7dc-6fd2dfc9c177@40
g2000prx.googlegroups.com>, Mon, 12 Jan 2009 15:05:38, Jorge
<(E-Mail Removed)> posted:
>On Jan 12, 11:36*pm, ArbolOne <(E-Mail Removed)> 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.
 
Reply With Quote
 
Jorge
Guest
Posts: n/a
 
      01-14-2009
On Jan 14, 5:46*pm, Dr J R Stockton <(E-Mail Removed)> wrote:
> In comp.lang.javascript message <4b3224c2-c5a4-453f-a7dc-6fd2dfc9c177@40
> g2000prx.googlegroups.com>, Mon, 12 Jan 2009 15:05:38, Jorge
> <(E-Mail Removed)> posted:
>
> >On Jan 12, 11:36*pm, ArbolOne <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Warning:Xst:382 - Register A is equivalent to B mag VHDL 1 05-19-2005 05:10 PM
instancename of current entity/architecture -- equivalent to C++ this??? Eric Peers VHDL 2 11-18-2004 05:23 PM
VHDL equivalent of verilog trireg Sanjeev VHDL 4 07-23-2004 09:55 AM
equivalent types in different packages Lolo VHDL 3 09-22-2003 03:23 PM
Re: Image Scanning - TWAIN equivalent Brendan Duffy ASP .Net 0 07-24-2003 08:29 AM



Advertisments