What is the equivalent of #include filename.hpp

A

ArbolOne

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
 
T

Thomas 'PointedEars' Lahn

ArbolOne said:
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
 
J

Jorge

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 !
 
R

Ralf Beutler

Hi,
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
 
D

Dr J R Stockton

In comp.lang.javascript message <4b3224c2-c5a4-453f-a7dc-6fd2dfc9c177@40
g2000prx.googlegroups.com>, Mon, 12 Jan 2009 15:05:38, Jorge
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.
 
J

Jorge

In comp.lang.javascript message <4b3224c2-c5a4-453f-a7dc-6fd2dfc9c177@40
g2000prx.googlegroups.com>, Mon, 12 Jan 2009 15:05:38, Jorge



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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top