Such an approach is illustrated by use of an example, an uptime checker (http://
ajaxref.com/ch2/connectioncheck.html). In this demonstration, the server-side program
called connectioncheck.php will generate an invisible image encoded with data. The
image height indicates the uptime status (up being 2 and down 1) and the width indicating
the rough roundtrip time in milliseconds. Upon receipt of the image, the height and width
are inspected and the appropriate message is displayed to the user.
FIGURE 2-5 Custom-generated image example
C h a p t e r 2 : P r e - A j a x J a v a S c r i p t C o m m u n i c a t i o n s Te c h n i q u e s 37
PART I
if (responseImage.height == "2")
target.innerHTML = "Server available. Connection time approximately "
+ responseImage.width + "ms.";
else
target.innerHTML = "Server unavailable.";
You can see the communication trace here, which illustrates this process.
This approach is very limited as there are only two dimensional values that have to be
encoded as simple integers that can be passed back. Fortunately, it turns out that if this
method is used in conjunction with a cookie, all sorts of data can be passed back to the
browser in response to an image request.
Images and Cookies Technique
To expand the image communication pattern to a truly valuable two-way method, we add
in a batch of cookie power.
Pages:
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93