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