in my.js file:
var req = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
function doRequest(url, fCallback)
{
req.open('GET', url);
req.onreadystatechange = fCallback;
req.send(null);
}
function loadPage()
{
doRequest("/get_user_info.pl", showUserInfo);
doRequest("/get_menu.pl", showMenu);
doRequest("/get_news.pl", showNews);
doRequest("/get_body.pl", showBody);
}
// end of example
But, when this function executes i get an error, because it starts new
request where previous was not finished.
How I make several requests in same time ?
you may want to create your xhmlHttpRequest only when needed, and not
when the script is loading. i.e
function createRequest(){
....
var req = (window.ActiveXObject) ? new
ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
...
return(XMLHttpRequest)
}
function doRequest(url, fCallback) {
var req = null
req = createRequest();
if(req == null) {
....
alert ("Error wile creating REQ ")
}
else{
req.open('GET', url);
req.onreadystatechange = fCallback;
req.send(null);
}
}
you may also look at examples available on the net
http://www.google.com/search?num=30&hl=en&newwindow=1&safe=off&q=xmlhttprequest+concurrent&btnG=Search
Best Regards
Jacques Jocelyn
> var req = (window.ActiveXObject) ? new
> ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
That way with IE 7 your code would create and use the ActiveXObject
instead of using XMLHttpRequest. Consider changing that to e.g.
var req = null;
if (typeof XMLHttpRequest != 'undefined') {
req = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
try {
req = new ActiveXObject('Msxml2.XMLHTTP');
}
catch (e) {
try {
req = new ActiveXObject('Microsoft.XMLHTTP');
}
catch (e2) {}
}
}
> But, when this function executes i get an error, because it starts new
> request where previous was not finished.
>
> How I make several requests in same time ?
You need to create one request object per request, for instance by
moving the creation code (e.g. new XMLHttpRequest()) into the doRequest
function.
On the other hand there are recommended limits on the number of
simultaneous requests a HTTP user agent is supposed to make so don't
expect that the browser makes lots of HTTP requests at the same time.
--
Martin Honnen
http://JavaScript.FAQTs.com/
function showMenu()
{
if (req.readyState == 4 && req.status == 200)
{
// do something with menu if state is ok
}
}
If I was created a request in function like doRequest() I cann't get
Request object in callback function. For example, in function above
object 'req' is not defined and I cann't check status of request. Is
it possible to get request object that
*Martin Honnen*
Hmmm.. I have a Vista intaslled and check my code in Firefox 2.x and
IE 7. All work good. But all the same thanks :)
P.S. Sorry for my english, i'm not englishman. :(
Is it possible to get request objectthat relates to this callback
function?
> Hmmm.. I have a Vista intaslled and check my code in Firefox 2.x and
> IE 7. All work good.
I did not say that the code does not work. However if you first check
for ActiveXObject then you don't use XMLHttpRequest with IE 7 although
it does support it and is supposed to improve performance. See
<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects/obj_xmlhttprequest.asp>