2

In all the AJAX libraries I've checked out, XMLHttpRequest involves a lengthy declaration with tests or try/catch statements.

I need to retrieve XML via a SOAP GET request, and I tested the following declaration successfully in IE7+, Firefox and Chrome:

var xhr=new XMLHttpRequest()||new ActiveXObject("Microsoft.XMLHTTP");

What am I missing here? Did I overlook some edge cases where my declaration is going to break?

Edit

So the second part of the declaration never runs. Does it means that for IE7+/Firefox/Chrome I just need:

var xhr=new XMLHttpRequest();
talnicolas
  • 13,885
  • 7
  • 36
  • 56
Christophe
  • 27,383
  • 28
  • 97
  • 140
  • Is it all inside a `try { ... }` block? – Pointy Dec 12 '11 at 21:05
  • er...no. IE7+, Firefox and Chrome apparently accepted it. – Christophe Dec 12 '11 at 21:07
  • As already shown in the first answers here, most of those lenghty declarations are there for good reason. I'd just use YUI, jQuery, or your other favorite JavaScript/AJAX library and be done with it. Why solve the problem again? :-) – ziesemer Dec 12 '11 at 21:09
  • As it turns out, I am trying to solve a problem that jQuery doesn't seem to address: http://stackoverflow.com/questions/8388889/ajax-library-that-supports-namespaced-xml – Christophe Dec 12 '11 at 21:15
  • I'd agree with PointedEars's direction on that question: the XMLHttpRequest should only be used as the transport mechanism, and a separate layer used for navigation of an XML tree. A few years ago, I created by own "XPathExpression" library that did just this - providing the basic XPath methods that worked with all XPath expressions (that I found) - needing only ~90 lines of code to handle the browser-specific differences - including use of XML namespaces. I'll see if I can get this code up-to-date and contributed as an answer to your other question. – ziesemer Dec 12 '11 at 21:23

2 Answers2

3

You gonna miss the case for Internet Explorer versions inferior to 7:

new ActiveXObject("MSXML2.XMLHTTP");

I think having a function createRequest() for example that you don't have to modify and returns the good object depending on the browser, and containing try/catch to handle errors would be a better way to go.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
1

new has precendence, so you're executing new XMLHttpRequest() and check the return value. However, in case XMLHttpRequest is not defined, this execution already throws an error. Rather, you want to check whether XMLHttpRequest is defined before applying new, e.g.:

var xhr = typeof XMLHttpRequest === "undefined"
            ? new ActiveXObject("Microsoft.XMLHTTP")
            : new XMLHttpRequest; // you can leave out the parens with no arguments
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • doh...thanks pimvdb! I have updated my question, it seems that XMLHttpRequest is enough for my case. – Christophe Dec 12 '11 at 21:38
  • 1
    @Christophe: You are correct by saying the second part (after `||`) does not add anything. `new` always returns an object, which is never falsy. – pimvdb Dec 12 '11 at 21:40
  • In my opinion, returning false would be more useful... Anyway thanks for the answer. – Christophe Dec 13 '11 at 02:03