Returning Zero-Length Images Works Just Fine

Posted September 10, 2007 at 7:14 PM by Ben Nadel

Tags: ColdFusion, Javascript / DHTML

NOTE: I have updated the Status Code returned by image.cfm to be 204 as per Justin's comments (see below). According to the w3c, 204 stands for No Content (which can/should be used in conjunction with the zero-length header).

I like AJAX a lot; I think it has a lot of uses. But, at the same time, I don't think it needs to be used every time you want to sent a message back to the server. AJAX is only a requirement if you actually care what (if anything) the server sends back as its response (or if you need to POST information vs. GET'ing it). In the past, if I didn't need to listen for a server response, I have advocated using a Javascript IMG object to ping the server.

The idea here is that you contact the server by setting an image's source equal to the ColdFusion page that you are trying to hit. Then, the server grabs all the URL params you send as the image SRC request and returns an image such as a 1x1 GIF image. However, what happens if you don't return an image at all? Why go through the overhead? Not returning an image has always made me nervous. Not sure why. It's not like the page is gonna explode or anything?

To put my mind at ease, I finally did a test to see what would actually happen if no image was returned to the Javascript image request. First, I put together a small XHTML page that would make an IMG src request to the server and then output whether the image loaded successfully:

  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  • <html>
  • <head>
  • <title>Zero-Length Image Test</title>
  •  
  • <script type="text/javascript">
  •  
  • function TestImage(){
  • var imgTest = new Image();
  • var objMessage = document.getElementById( "message" );
  •  
  •  
  • // Set up load handler.
  • imgTest.onload = function(){
  • objMessage.innerHTML = (
  • "Loaded: " +
  • imgTest.complete
  • );
  • }
  •  
  •  
  • // Set up error handler.
  • imgTest.onerror = function(){
  • objMessage.innerHTML = (
  • "Error: " +
  • imgTest.complete
  • );
  • }
  •  
  •  
  • // Set the image source.
  • imgTest.src = "image.cfm";
  •  
  • return;
  • }
  •  
  • </script>
  • </head>
  • <body onload="TestImage();">
  •  
  • <h1>
  • Zero-length Image Test
  • </h1>
  •  
  • <p>
  • <span id="message"></span>
  • </p>
  •  
  • </body>
  • </html>

Notice here that source of the image is the images.cfm ColdFusion template. This would theoretically allow us to pass variables to the server via the IMG source URL.

Then, I put together a small ColdFusion page that returns nothing. And, not only does it return nothing, it tells the browser, using the ColdFusion CFHeader tag, to expect zero bytes of content. This should get the browser to stop downloading the requested file immediately (what we want).

  • <!--- Kill extra output. --->
  • <cfsilent>
  • <!---
  • Tell the browser that the image was found but that
  • it has no content. Status 204 means no content.
  • --->
  • <cfheader
  • statuscode="204"
  • statustext="No Content"
  • />
  •  
  • <!--- Tell the browser that the image has no content. --->
  • <cfheader
  • name="content-length"
  • value="0"
  • />
  •  
  • <!--- Set the content type. --->
  • <cfcontent
  • type="text/gif"
  • reset="true"
  • />
  •  
  • </cfsilent>

Running the code above, we get the following output:

Zero-length Image Test

Error: true

Awesome - the world didn't stop spinning! Returning a zero-length image seems to work perfectly well; doesn't launch any Javascript errors; doesn't mess up the browser at all. All it does is trigger the OnError handler of the image which shows the "complete" flag as being true. I am very OK with this, and it makes me feel very comfortable to use a Javascript IMG object as a no-overhead server request method.




Reader Comments

Sep 10, 2007 at 9:30 AM // reply »
25 Comments

Hi Ben, you could also pass back a status code of 204 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5) which tells the client that the request was successful but that it has no data to return... I've been using this technique ever since Brandon Harper had wrote about in the CFDJ a few years back (http://www.sys-con.com/story/?storyid=46789&de=1)


Sep 10, 2007 at 9:40 AM // reply »
11,238 Comments

@Justing,

Awesome tip! I rare use anything other that 200, 500, 404, 301 and 302. Good to know there is one like that (that stands for no data). I will modify the post to reflect this (after I test it).


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools