473,395 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

confusion!?

Hello,

after reading several AJAX samples I wonder why one can found the following
lines in quite many of them:

....
else if (window.ActiveXObject) {
request = new ActiveXObject("Msxml2.XMLHTTP");
if(! request) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
....

Microsoft states that XmlHTTPRequest was added in MSXML2.

Best Regards,

Oliver

Jul 13 '06 #1
7 1442
Oliver Block said the following on 7/13/2006 9:20 AM:
Hello,

after reading several AJAX samples I wonder why one can found the following
lines in quite many of them:

....
else if (window.ActiveXObject) {
request = new ActiveXObject("Msxml2.XMLHTTP");
if(! request) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
....

Microsoft states that XmlHTTPRequest was added in MSXML2.
And if the browser doesn't have MSXML2?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 13 '06 #2
window.XMLHttpRequest will be available in IE 7

I usually use the following in a getRequest() function, which will
return null if no request object is available

if( window.XMLHttpRequest )
{
return new XMLHttpRequest();
}

var req_types = [ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ];
for( var i=0; i<req_types.length; ++i )
{
try
{
var xmlrequest = new ActiveXObject( req_types[ i ] );
return xmlrequest;
}
catch( e ) { }
}

return null;

Oliver Block wrote:
Hello,

after reading several AJAX samples I wonder why one can found the following
lines in quite many of them:

...
else if (window.ActiveXObject) {
request = new ActiveXObject("Msxml2.XMLHTTP");
if(! request) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
...

Microsoft states that XmlHTTPRequest was added in MSXML2.

Best Regards,

Oliver
Jul 13 '06 #3
Randy Webb wrote:
Oliver Block said the following on 7/13/2006 9:20 AM:
>Hello,

after reading several AJAX samples I wonder why one can found the
following lines in quite many of them:

....
else if (window.ActiveXObject) {
request = new ActiveXObject("Msxml2.XMLHTTP");
if(! request) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
....

Microsoft states that XmlHTTPRequest was added in MSXML2.

And if the browser doesn't have MSXML2?
It does will not provide an XMLHTTPRequest-Object!?

-- Oliver
Jul 14 '06 #4
Rik
bobzimuta wrote:
window.XMLHttpRequest will be available in IE 7

I usually use the following in a getRequest() function, which will
return null if no request object is available

if( window.XMLHttpRequest )
{
return new XMLHttpRequest();
}

var req_types = [ 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ];
for( var i=0; i<req_types.length; ++i )
{
try
{
var xmlrequest = new ActiveXObject( req_types[ i ] );
return xmlrequest;
}
catch( e ) { }
}

return null;
Another solution:

if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
var msxmls = new Array(
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP');
for (var i = 0; i < msxmls.length; i++) {
try {
return new ActiveXObject(msxmls[i]);
} catch (e) {
}
}
return false;
};
}

Which would mean in the rest of the code you can just use:

request = new XMLHTTPRequest;
if(!request){
//errorhandling, fallbacks etc.
} else {
//go on
}

Grtz,
--
Rik Wasmus
Jul 14 '06 #5
Rik wrote:
bobzimuta wrote:
<snip>
if (window.ActiveXObject && !window.XMLHttpRequest) {
window.XMLHttpRequest = function() {
var msxmls = new Array(
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP');
for (var i = 0; i < msxmls.length; i++) {
try {
return new ActiveXObject(msxmls[i]);
} catch (e) {
}
You realise that if you do that when, say, Msxml2.XMLHTTP is the
version used your code will try three alternatives which will error
prior to returning the object, and it will do that on each and every
occasion the replacement - XMLHttpRequest - constructor is used.
}
return false;
Returning false form a function used as the operand of the - new -
operator does not result in boolean false being returned, instead the
Native ECMAScript object that is being constructed is returned here.
};
}

Which would mean in the rest of the code you can just use:

request = new XMLHTTPRequest;
if(!request){
//errorhandling, fallbacks etc.
Because when IE cannot create an XML HTTP request object (for example
when ActiveX is disabled) your - new XMLHTTPRequest - expression
evaluated as a Native ECMAScript object, and such an object
type-converts to true in and - if - expression, this "errorhandling,
fallbacks etc." code will _never_ be executed, and instead the code
will take the other branch and treat the Native ECMAScript object as an
XML HTTP request object, erroring out as soon as it tries to call a
method of the object.
} else {
//go on
}

function XMLHttpRequestFactory(){
var msxmls, comString, xmlObj = null, index;
if(
(window.XMLHttpRequest)&&
(xmlObj = (new XMLHttpRequest()))
){
/* Replace the factory with an version uses the available -
XMLHttpRequest - constructor:-
*/
XMLHttpRequestFactory = function(){
return new XMLHttpRequest();
};
}else if(window.ActiveXObject){
msxmls = [
'Msxml2.XMLHTTP.5.0',
'Msxml2.XMLHTTP.4.0',
'Msxml2.XMLHTTP.3.0',
'Msxml2.XMLHTTP',
'Microsoft.XMLHTTP'
];
for(index = 0;index < msxmls.length;++index){
try{
if((xmlObj = new ActiveXObject(msxmls[index]))){
comString = msxmls[index];
/* Replace the factory with an version that directly used
the successful string, as determined by its being the
string that resulted in the creation of the XML HTTP
request object without any exceptions being thrown:-
*/
XMLHttpRequestFactory = function(){
return new ActiveXObject(comString);
};
msxmls = null;
break;
}
}catch(e){;}
}
if(!xmlObj){
/* Replace the factory with a dummy that just returns null as
there is no mechanism for creating the XML HTTP request
objects in this environment:-
*/
XMLHttpRequestFactory = function(){
return null;
};
}
}else{
/* Replace the factory with a dummy that just returns null as there
is no mechanism for creating the XML HTTP request objects in
this environment:-
*/
XMLHttpRequestFactory = function(){
return null;
};
}
return (function freeRefs(arg){
xmlObj = null; //guarantee that any ActiveX object is not stored in
//this closure.
return arg;
})(xmlObj);
}

Richard.

Jul 14 '06 #6
Oliver Block said the following on 7/14/2006 8:51 AM:
Randy Webb wrote:
>Oliver Block said the following on 7/13/2006 9:20 AM:
>>Hello,

after reading several AJAX samples I wonder why one can found the
following lines in quite many of them:

....
else if (window.ActiveXObject) {
request = new ActiveXObject("Msxml2.XMLHTTP");
if(! request) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
....

Microsoft states that XmlHTTPRequest was added in MSXML2.
And if the browser doesn't have MSXML2?

It does will not provide an XMLHTTPRequest-Object!?
So, if the browser doesn't have the Msxml2.XMLHTTP ActiveX component but
it does have the Microsoft.XMLHTTP ActiveX component then it won't
provide the object? That second if section is a backstep of sorts to
support browsers that have one ActiveX component but not the other. And
there are more than 2 of them.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 14 '06 #7
Randy Webb wrote:
Oliver Block said the following on 7/14/2006 8:51 AM:
>Randy Webb wrote:
>>Oliver Block said the following on 7/13/2006 9:20 AM:
Hello,

after reading several AJAX samples I wonder why one can found the
following lines in quite many of them:

....
else if (window.ActiveXObject) {
request = new ActiveXObject("Msxml2.XMLHTTP");
if(! request) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}
}
....
That second if section is a backstep of sorts to
support browsers that have one ActiveX component but not the other. And
there are more than 2 of them.
I found out that IE 5 and IE 5.5 without any further MSXML just support the
second. But won't ActiveXObject throw an exception if the first one doesn't
return a request object?

Oliver

Jul 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Doug Farrell | last post by:
Hi all, I'm trying to do the following from within a code module: import re # text to match text = "Good morning x something /x, how are you today x something else /x"
1
by: Mathias Mamsch | last post by:
Hi, I have some confusion concerning the weakref module. I am trying to save a weak reference to a bound member function of a class instance for using it as a callback function. But I always...
38
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages...
10
by: Kamilche | last post by:
I'm trying to pack two characters into a single byte, and the shifting in Python has me confused. Essentially, it should be possible to use a 'packed string' format in Python, where as long as...
4
by: JMCN | last post by:
object invalid or no longer set - confusion of the recordset in access 2003. i am currently converting from access 97 to access 2003. majority of the codes converted over perfectly fine, though...
0
by: i_have_control | last post by:
I'd be grateful for any input on this one: I have three web domains. The destinations of two are set to folders on the first, though that fact is transparent to the user (i.e: it does not...
13
by: Steve | last post by:
I have a form with a dataset and a datagrid. I created a dataview on this dataset. When the user modifies the datagrid, I look up this record in the dataview to make sure it is unique. Here is...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
1
by: Richard Lewis Haggard | last post by:
I'm having a problem with what appears to be some sort of confusion with references. I have a single solution with a dozen projects which has been working quite nicely for a while. The references...
2
by: Riaaaa | last post by:
Hello, We are doing the project in VB.Net. We had a great confusion for ASP.Net and VB.Net. Is VB.Net project performed in Microsoft Visual Studio 2005 ?? We have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.