// Listing 1
function LoadLib_XHR (Args)
{
var XHRWrapper = null;
var Timer = null;
function OnTimeout ()
{
if (XHRWrapper) { XHRWrapper.Abort (); }
if (Args.OnTimeout) { Args.OnTimeout (); }
}
function OnError (E)
{
clearTimeout (Timer);
if (Args.OnError) { Args.OnError (E); }
else { throw E; }
}
function OnLibLoaded (XHRObj)
{
clearTimeout (Timer);
try
{
if (window.execScript) { window.execScript (XHRObj.responseText); }
else { this.eval ? this.eval (XHRObj.responseText)
: eval (XHRObj.responseText); }
}
catch (E)
{
if (Args.OnError) { Args.OnError (E); }
else { throw E; }
}
}
try
{
XHRWrapper = XHRFactory.CreateXHRWrapper ();
if (Args.TimeoutDelay) { Timer = setTimeout (OnTimeout, Args.TimeoutDelay); }
else { Timer = setTimeout (OnTimeout, 3000); }
XHRWrapper.DoTxn (Args.LibURL, "GET", false, OnLibLoaded, null, null, OnError);
}
catch (E) { OnError (E); }
}
// Example 1: Structure of object that must be passed to LoadLib_XHR
var Args = // Does not have to be called 'Args'
{
LibURL : ... // Mandatory member. Denotes the URL of the library
TimeoutDelay : ... // Optional member. Must be an integer if included.
// Overrides the default delay of 3000 milliseconds
OnTimeout : ... // Optional member. If included, must refer to a
// call-back that is invoked if the XHR transaction
// times out
OnError : ... // Optional member. If included, must refer to an
// error-handler call-back that is invoked if the HTTP
// transaction fails
};
// Example 2: Passing just the URL
LoadLib_XHR ( { LibURL : "MyLib.js" } );
// Example 3: Passing the URL and a Timeout delay
LoadLib_XHR ( { LibURL : "MyLib.js", TimeoutDelay : 2000 } );
// Example 4: Passing the URL, Timeout and Error Callbacks
function OnLibLoadTimeout () { alert ("Library-load timed out"); }
function OnLibLoadError (E) { alert (E.message); }
LoadLib_XHR ( { LibURL : "MyLib.js",
TimeoutDelay : 2000,
OnTimeout : OnLibLoadTimeout,
OnError : OnLibLoadError } );
// Example 5: Out-of-line argument-object definition
function OnLibLoadTimeout () { alert ("Library-load timed out"); }
function OnLibLoadError (E) { alert (E.message); }
var Args =
{
LibURL : "MyLib.js",
TimeoutDelay : 5000,
OnTimeout : OnLibLoadTimeout,
OnError : OnLibLoadError
};
LoadLib_XHR (Args);
// Example 6: Constructor-based argument-object creation
function OnLibLoadTimeout () { alert ("Library-load timed out"); }
function OnLibLoadError (E) { alert (E.message); }
function LoadLibArgs (LibURL, TimeoutDelay, OnTimeout, OnError)
{
this.LibURL = LibURL;
this.TimeoutDelay = TimeoutDelay;
this.OnTimeout = OnTimeout; // Call back functions could
this.OnError = OnError; // be defined anonymously here
};
var Args = new LibLoadArgs ("MyLib.js", 2000, OnLibLoadTimeout, OnLibLoadError);
LoadLib_XHR (Args);