Require Once Class Loader

B

blaine

Hey,

I'm trying to do a loading routine that would act like php's require
once. The reason for this is I have a few classes that may or may not
be used on a page. If more then one is used then some of them require
similar base objects.

Anyhow, below was my first idea of a require once loader.. Check the
this variable (window object) for the class name you are looking for
and if it does not exists then load the class. If it does then skip
the class.

In theory it seems good.. In practice it never finds any of the loaded
classes. Any ideas?


// file1 names also indicate function name
//ie file1.js contains var file1 = function();

var files = new Array('file1','file2','file3');
var files2 = new Array('file1','file4');


function loadFiles( files ){

for (var i=0; files.length > i; i++){
//check to see if the class is already loaded.
if ( key in this == false ){
document.write("<SCR" + "IPT LANGUAGE='JavaScript' SRC='"
+ path + files + ".js' TYPE='text/javascript'><\/SCR" + "IPT>");
}
}
}


loadFiles( files );
loadFiles( files2 );
 
T

Thomas 'PointedEars' Lahn

I'm trying to do a loading routine that would act like php's require
once. The reason for this is I have a few classes that may or may not
be used on a page. If more then one is used then some of them require
similar base objects.

There are no classes. You are using an OOL that uses prototype-based
inheritance.
Anyhow, below was my first idea of a require once loader.. Check the
this variable (window object) for the class name you are looking for
and if it does not exists then load the class. If it does then skip
the class.

In theory it seems good.. In practice it never finds any of the loaded
classes. Any ideas?

Did you post what you used? Because I don't see `key' being declared or
defined; the code below would fail anyway.
// file1 names also indicate function name
//ie file1.js contains var file1 = function();

var files = new Array('file1','file2','file3');
var files2 = new Array('file1','file4');


function loadFiles( files ){

for (var i=0; files.length > i; i++){
//check to see if the class is already loaded.
if ( key in this == false ){
document.write("<SCR" + "IPT LANGUAGE='JavaScript' SRC='"

There is no need to separate "<SCR" and "IPT", or for the deprecated
`language' attribute.
+ path + files + ".js' TYPE='text/javascript'><\/SCR" + "IPT>");


There is no need to separate "<\/SCR" and "IPT" as long as you write "<\/"
instead of "</" (thereby escaping the otherwise recognized ETAGO delimiter).
}
}
}

loadFiles( files );
loadFiles( files2 );

I really don't see a purpose in all of this.

If client-side script support is present and enabled, using loadFiles()
within the global context merely adds overhead (and triggers the NRLB);
using it in local context (triggers the NRLB and) usually overwrites the
document because when that is executed, the document is already loaded.

If client-side script support is absent or disabled, the script that writes
the `script' elements will not be executed.

That said, trying to load external script resources after parsing has
completed, is Voodoo programming as there is no reference material that
states it can be done.


PointedEars
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top