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,314 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
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools