confusion!?

Discussion in 'Javascript' started by Oliver Block, Jul 13, 2006.

  1. Oliver Block

    Oliver Block Guest

    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
    Oliver Block, Jul 13, 2006
    #1
    1. Advertising

  2. Oliver Block

    Randy Webb Guest

    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/
    Randy Webb, Jul 13, 2006
    #2
    1. Advertising

  3. Oliver Block

    bobzimuta Guest

    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
    bobzimuta, Jul 13, 2006
    #3
  4. Oliver Block

    Oliver Block Guest

    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
    Oliver Block, Jul 14, 2006
    #4
  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);
    > } 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.
    Richard Cornford, Jul 14, 2006
    #5
  6. Oliver Block

    Randy Webb Guest

    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/
    Randy Webb, Jul 14, 2006
    #6
  7. Oliver Block

    Oliver Block Guest

    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
    Oliver Block, Jul 15, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. martin f. krafft

    confusion when resetting registers

    martin f. krafft, Aug 18, 2004, in forum: VHDL
    Replies:
    2
    Views:
    467
    Paul Sereno
    Aug 19, 2004
  2. Peter Hermansson

    Procedures in testbench confusion

    Peter Hermansson, Aug 25, 2004, in forum: VHDL
    Replies:
    2
    Views:
    865
    Peter Hermansson
    Aug 25, 2004
  3. Chris

    defined() confusion

    Chris, Dec 11, 2003, in forum: Perl
    Replies:
    2
    Views:
    508
  4. Jerome

    runtime confusion

    Jerome, Dec 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    400
    Jerome
    Dec 3, 2005
  5. Chris Jackson

    MissingMethodException Confusion

    Chris Jackson, Jul 11, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    1,808
    Luke Zhang [MSFT]
    Jul 17, 2003
Loading...

Share This Page