views:

423

answers:

4

I've created a service that helps with error pages in web apps via an snippet of code that is copy/pasted in to the error page templates (a la Google Analytics). The status code is set to a hidden input within the installation code. The problem is that I don't want the installation code to be dependent on a server-side language (e.g. PHP, Ruby) - but the app could really, really use a dynamic method of collecting the status code in the http request without having hard coded status codes and, therefore, a necessity for separate installation codes for each error page (although, we're really only looking at 500s & 404s; but one option is too many, as far as I'm concerned).

Quizzing Google and my dev friends suggests that getting the status code via javascript isn't going to be possible (we're getting the http_referer that way, though) but I wondered if anyone had any suggestions that I haven't come across yet...

+1  A: 

With XmlHttpRequest, you can just retrieve the contents of a text/plain file located on your server that will be independant from the server language you're using.

This could be a text file with values separated by commas or semicolons that you'll split once the request is done.

Note : You'll have to use the responseText property of the XmlHttpRequest.

Hope I'm clear enough.

SleepyCod
Thanks SleepyCod. The XmlHttpRequest is new to me. Am I right in assuming that there would be a javascript include in the DOM which assigns the XmlHttpRequest.reponseText to value of the hidden input? The http referer is being obtained using the following method (and I was wondering if the XmlHttpRequest.responseText could be assigned in the same way;<script type="text/javascript"> if(document.referrer) document.getElementById('np_referrer').value = document.referrer;</script>
A: 

Neil.

Here's some code to clarify.

function createXhrObject()
{
    if (window.XMLHttpRequest)
        return new XMLHttpRequest();

    if (window.ActiveXObject)
    {
        var names = [
            "Msxml2.XMLHTTP.6.0",
            "Msxml2.XMLHTTP.3.0",
            "Msxml2.XMLHTTP",
            "Microsoft.XMLHTTP"
        ];
        for(var i in names)
        {
            try{ return new ActiveXObject(names[i]); }
            catch(e){}
        }
    }
    return null; // not supported
}
xhr = createXhrObject();

xhr.onreadystatechange = function() 
{
    if(xhr.readyState == 4 && xhr.status == 200) 
    {
        document.getElementById('your_hidden_input').value = xhr.responseText;
    } 
}

xhr.open('GET', 'your_text_file.txt', true); 
xhr.send(null);
SleepyCod
A: 

As far as I know, there's no way to figure out the status code of a page itself using code embedded on that page. (An Ajax request to a URL will return the status code, so theoretically you could to an Ajax request to the page you're on (i.e request it twice), but that's a bit wacky if you need to do it on every page load...)

I think the only solution is to do as you suggest and modify your server-side 404, 500, etc. handlers to embed information about the status code within the page itself (e.g. <script>document.status = "404";</script>), which you can then extract with JavaScript.

mjs
A: 

There is no way of accessing the HTTP status code from within a HTML page or from a JavaScript function. XMLHttpRequest can access it, because it is aware of the Http request it has created on its own. It is the originator of the HTTP connection in the first place. But the JavaScript code you want to run is contained in a page which has already been loaded.

Btw, this would mix layers (e.g. transport layer and application layer) anyway. For example, since you can load a .html page from filesystem - what is the HTTP status code in this case? Or what is the HTTP status code if you're accessing a HTML page from an FTP server or from a SVN Repository?

mhaller