views:

78

answers:

2

Can I evaluate the status of a URL on a remote server with JavaScript? Is it considered a cross-domain reference even if I don't get the actual contents of a document?

If it's not possible through plain JavaScript, could I perhaps load the document in an iframe?

What I would like to do is to check if the HTTP status code is 200 and if the Content-Type is text/xml, to make sure the URL a user entered is valid.

I'm using YUI btw.

+4  A: 

Can I evaluate the status of a URL on a remote server with JavaScript?

Not in a standard browser security context (which I'll assume unless you say otherwise as it covers almost all use cases).

Is it considered a cross-domain reference even if I don't get the actual contents of a document?

Yes.

If it's not possible through plain JavaScript, could I perhaps load the document in an iframe?

No, and even if you could you wouldn't have access to the HTTP headers.

David Dorward
That's a shame, but thanks anyway.
Jacob R
+2  A: 

You're right in your assumptions that any XMLHTTPRequest to a non-local path is considered cross-domain, regardless of its nature.

That's not to say it can't be done. For example, you could route your request through a local server-side script (ie. PHP/Python/Ruby et al) that connects to a remote host, retrieves data and outputs it. It may then be accessed by JavaScript, effectively allowing you to make a cross-domain request with AJAX.

Johannes Gorset
+1 Probably not recommended at all, but would definitely work :P
jaywon
You're right, though it's only a security risk if said script will happily connect to any host and retrieve any data.
Johannes Gorset