Mark wrote:
Is there a way to achieve multithreading in JavaScript? I'm looking to
fetch a page into a div while allowing the user to interact with another
div. At some point the newly fetched page contents will be available to the
div that the user is working in but I don't want to cause unnecessary
blocking. I've thought of using frames (would prefer divs) plus timeouts
and checking for when a load completes. Does anyone have an idea of how
this could work?
Client side JavaScript so far doesn't have any threading constructs but
it is event based and you can start loading an image for instance with
var img = new Image();
img.src = 'whatever.gif';
which the browser usually then does in a thread of its own and if you
have registered an onload event listener e.g
var img = new Image();
img.onload = function (evt) {
alert(this.src + ' loaded.');
};
img.src = 'whatever.gif';
then your event listener will be called once the image is loaded.
Some browsers like IE5+/Win and Netscape 6/7 and Mozilla based browsers
have special objects allowing you to make asynchronous HTTP requests and
set up event listeners to handle the response:
var httpRequest;
if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject(' Msxml2.XMLHTTP' );
}
else if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest( );
}
if (httpRequest) {
//open(httpReques tMethod, URL, async)
httpRequest.ope n('GET', 'whatever.html' , true);
httpRequest.onr eadystatechange = function () {
if (httpRequest.re adyState == 4) {
alert(httpReque st.responseText );
}
};
httpRequest.sen d(null);
}
That being demonstrated you should first consider making a site that
simply loads your page into an HTML iframe as that doesn't depend on
scripting being supported and enabled and should give you all the
advantages of interactivity and threaded request/response processing.
--
Martin Honnen
http://JavaScript.FAQTs.com/