I'm trying to implement fire-and-forget on an img src=... call from a web page, and am looking for the most solid method. Why?
In most Web tracking systems like Omniture or Coremetrics, there is a request for an img src which also happens to carry all the data for the tracking system to record. Like any other image request, the page tries to wait for the response, potentially slowing down the page-load.
What I'm trying to do is implement a tracking system where webpage response time is a much higher priority than whether the request was actually successful, and therefore waiting for the response is totally unnecessary under all conditions, which include...
- The server received the request, and can readily respond (HTTP response code 200)
- The server received the request, but is bogged down and can still readily respond with an error 500 response code
- The server is not running at all
- The server was not found due to DNS glitches
- The request hit the server, but is so bogged down that it's taking noticeable time to respond.
I have already investigated using the XMLHttpResponse object and an asynchronous request that can quietly fail, but you quickly run into the same origin policy and browser compatibility code bloat. I believe a plain jane img request is still the best request mechanism, but am trying to work out the best fire-and-forget implementation with the smallest amount of code. So far, I know I can...
onerror="this.parentNode.removeChild(this)" onload="this.parentNode.removeChild(this)"
This works pretty well on scenarios 1 through 4 where there is a finite and predictable conclusion to the request. It breaks down in scenario 5, which could be avoided if there were something in-between.
I'm toying with the idea of seeing if this.src.readyState == "complete" but see that I am heading down a road of browser compatiblity and race-condition hell were it would be wise to tap the StackOverflow community first.
Anyone have experience with this and a good solution?