Question : Where to create XmlHTTPRequest object

Hello!

Usually when I look at some AJAX examples I see that XmlHTTPRequest object is created is same function that is called on AJAX triggering event. For example:

funciton doSth()
{
  xmlHtttp = new XmlHTTPRequest();
  xmlHttp.open ...
  xmlHttp.send ...
}
...


I was wondering I can create such object only once at page start, then in my function doSth use instance created earlier.

I tested it and it's working, however I'm wondering if there are some problems that can happen with such usage ...

Answer : Where to create XmlHTTPRequest object

No problem at all doing that.
Most of modern browses even have a static instance of XmlHTTPRequest already present in the window object and is accessible through window.XMLHttpRequest
More information on that here: http://msdn.microsoft.com/en-us/library/ms535157(VS.85).aspx

So you can check if this object exist and only create your own if it doesn't
Random Solutions  
 
programming4us programming4us