Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > Other ASP.NET > BOOK: Beginning Ajax with ASP.NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Ajax with ASP.NET
This is the forum to discuss the Wrox book Beginning Ajax with ASP.NET by Wallace B. McClure, Scott Cate, Paul Glavich, Craig Shoemaker; ISBN: 9780471785446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Ajax with ASP.NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 20th, 2006, 09:13 PM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default XMLHttpRequest object instantiation

There is following code on page 79
Code:
try
{
xmlHttpObj = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e)
{
xmlHttpObj = new ActiveXObject(“Msxml2.XMLHTTP”);
}
Is it a correct sequence to detect available version of MSXML parser?
I believe that in case Microsoft.XMLHTTP ProgID is not registered there can hardly be Msxml2.XMLHTTP on the system. I think the sequence should be in reverse order. Am I wrong?
And probably the alternative approach presented on the same page that checks support for ActiveXObject first is not practical because it forces IE7 to use ActiveXObject instead of its XMLHttpRequest object (because it supports both variants).

__________________
Alexey Gorkov
  #2 (permalink)  
Old September 23rd, 2006, 05:23 AM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

In the book "Professional Ajax" (also Wrox) they create the XMLHttpRequest object like this:

function createXMLHttp() {
    var aVersions = [ "MSXML2.XMLHttp.5.0",
        "MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
        "MSXML2.XMLHttp","Microsoft.XMLHttp"
    ];

    for (var i = 0; i < aVersions.length; i++) {
        try {
            var oXmlHttp = new ActiveXObject(aVersions[i]);
            return oXmlHttp;
        } catch (oError) {
            //Do nothing
        }
    }
    throw new Error("MSXML is not installed.");
}

I’m not sure what the best approach is, though. I use the following:

var globalXMLHttpRequestObj = null;
function createXMLHttpRequestObject()
{
    if (globalXMLHttpRequestObj != null)
        return globalXMLHttpRequestObj;

    if (window.XMLHttpRequest)
        return globalXMLHttpRequestObj = new XMLHttpRequest();
    else
    {
        var aVersions = [ "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.5.0",
            "MSXML2.XMLHttp.4.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp",
            "Microsoft.XMLHttp" ];

        for (var i = 0; i < aVersions.length; i++)
        {
            try
            {
                globalXMLHttpRequestObj = new ActiveXObject(aVersions[i]);
                return globalXMLHttpRequestObj;
            }
            catch (oError)
            {
                //Do nothing
            }
        }
        throw new Error("Can't create XMLHttpRequest object.");
    }
}

  #3 (permalink)  
Old September 23rd, 2006, 09:31 AM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

DanJ,
Thanks for your reply. Well, I've read Professional Ajax (several times actually :) and I know the method presented there.
What I'd asked here about wasn't actually a question, rather a statement and I just wanted to hear from authors regarding this subject. To put it more precisely I think that both code snippets in “Try it out” section on page 79 are erroneous because of reasons presented earlier. I could be wrong of course. My apologies if it is the case.
And two additional remarks: probably it is not a good idea to name the method CreateXmlHttpRequestObject(). I don’t think anyone would use such a verbose name in real project. And it also probably not quite accurate to place such method in a global namespace (i.e. declare it globally) so despite of the phrase “The preceding example, which created the XmlHttpRequest object in a cross-browser fashion will be
encapsulated in a function called CreateXmlHttpRequestObject() and included in a script file named
CommonAJAXLibrary.js.” this method is not actually encapsulated.

  #4 (permalink)  
Old September 24th, 2006, 11:58 PM
Registered User
 
Join Date: Sep 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to coderevo
Default

Quote:
quote:Originally posted by AGS
 DanJ,
... so despite of the phrase “The preceding example, which created the XmlHttpRequest object in a cross-browser fashion will be encapsulated in a function called CreateXmlHttpRequestObject() and included in a script file named CommonAJAXLibrary.js.” this method is not actually encapsulated.
Why do you consider this not to be encapsulated?
  #5 (permalink)  
Old September 25th, 2006, 04:52 AM
AGS AGS is offline
Wrox Technical Editor
Points: 267, Level: 5
Points: 267, Level: 5 Points: 267, Level: 5 Points: 267, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2005
Location: Russia
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Default

coderevo,

Function is certainly a type of encapsulation because it combines a set of instructions but it is probably the most basic level of encapsulation.
What I meant is (and I think it was clearly defined in the same sentence) that it would be better to use encapsulation on an object level, combining related functionality and data pertaining to XMLHttp request object and not to define such a function globally polluting the global namespace. Sorry if I am wrong.
But isn't it better to present real world examples? Have you seen such kind of standalone function used in any of existing JavaScript/Ajax frameworks/libraries?

 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
ResponseText property of XMLHTTPRequest Object prashantshekhar BOOK: Beginning Ajax with ASP.NET 1 August 28th, 2009 08:28 AM
ResponseXML property of XMLHTTPRequest Object prashantshekhar BOOK: Beginning Ajax with ASP.NET 0 August 14th, 2007 01:49 AM
object instantiation madness GilkesM BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 9 March 10th, 2007 01:40 AM
IE7 native XMLHttpRequest object ChrisScott Ajax 1 November 9th, 2006 04:36 AM
Dynamic object instantiation trisonics C# 2 July 29th, 2005 12:20 AM



All times are GMT -4. The time now is 07:04 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.