views:

391

answers:

3

I'm working on some Javascript that makes use of Firefox 3.5's ability to perform cross-domain XMLHttpRequests… But I'd like to fail gracefully if they aren't supported.

Apart from actually making a cross-domain request, is there any way to detect a browser's support for them?

+4  A: 

According to http://hacks.mozilla.org/2009/07/cross-site-xmlhttprequest-with-cors/ you should be able to use:

if ('withCredentials' in new XMLHttpRequest()) {
    /* supports cross-domain requests */
}

(Note: there is a comment on that page that Chrome 2 fails this test [although it does support cross-domain requests]. I tested Chrome 3 and the test is now passing.)

Keep in mind that just because the browser might support the cross-domain API does not mean the target server will allow the request to complete.

Crescent Fresh
+1  A: 

You might want to look at EasyXDM, which wraps cross-browser quirks and provides an easy-to-use API for communicating in client script between different domains using the best available mechanism for that browser (e.g. postMessage if available, other mechanisms if not).

Clearly that library has solved the browser-capabilities detection problem, so you can benefit from their experience. :-)

Justin Grant
+1  A: 

IE8 also has XDomainRequest object that can be used to retrieve RSS as text which can later be parsed into DOM.

Sergey Ilinsky