I'm working on an HTA application for the office (small user group, IE 6
only). It's very heavily script-based and I'm using a script loader utility
to populate the scripts in an initialization function.
(Some of the scripts use objects previously instantiated in the init
function - so I can't use simple <script> tags as easily.)
This works perfectly in almost all cases, however when calling the HTA from
a web server (rather than locally) it seems to have a problem with the
loading sequence: scripts begin loading, but don't finish before they are
called - causing an error.
I've been attempting to use the readyState property to determine loading
state, but it's been frustrating to say the least. I'd really like this
function to return ONLY when the script is ready for use, but so far any
check I try tends to lock the application (a simple "while" loop seems to
block other processing so the load never finishes for example).
I've love to be able to say "wait for 100ms, then check again", but
JavaScript doesn't have the concept of "Sleep()" and "setTimeout()" doesn't
prevent processing from continuing.
I know that I can create a se of cooperative functions to do the wait (by
bouncing between each other until the load is complete, the continuing) but
I was really hoping to create a single generically applicable loader
function. I want to call it, then have the script - without it having to
know anything about the application or the content of the script being
loaded.
Any ideas? Better yet, any solutions?
This is the loader function (the first block is an existence check, which
doesn't really need to there - but it prevents the app from uploading only
some of the cached scripts and eliminates a call to the server):
// Function to dynamically load scripts and add them to the document
function LoaderScript(ScriptSrc) {
var ScriptExists;
// Determine if the script has already been loaded
var ScriptCount = document.getElementsByTagName("script").length;
for ( var Cnt = 0; Cnt < ScriptCount; Cnt++ ) {
var CurScript =
document.getElementsByTagName("script")[Cnt];
if ( CurScript.src == ScriptSrc ) {
ScriptExists = true;
break;
} else {
ScriptExists = false;
};
};
if ( !ScriptExists ) {
var NewScript = document.createElement("script");
NewScript.src = ScriptSrc;
NewScript.type = "text/javascript";
NewScript.language = "JavaScript1.2";
document.getElementsByTagName("head")[0].appendChild(NewScript);
};
};
Thanks in advance,
Jim Davis
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Gold Sponsor - CFHosting.net
http://www.cfhosting.net
Message: http://www.houseoffusion.com/lists.cfm/link=i:5:138382
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/5
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:5
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.5
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54