Chapter 2: Pre-Ajax JavaScript Communications Techniques 23
PART I
Prior to calling sendRequest, the transport value in the payload is set to indicate the
particular communication being used. You can run the complete example at http://ajaxref
.com/ch2/onewayscript.html.
Other Approaches
If you haven’t guessed by now, just about any tag that can be set to reference a URL might be
a candidate for simple one-way communication. It is even possible to use cookies to transport
something up. While it is unlikely that there will be a need to use these techniques, they
should give you a good appreciation for all the possible methods to implement JavaScript-
based communications.
One-way Style Sheet Requests
The <link> tag can be used to associate a style sheet by setting its href attribute which will
then trigger a network request. No response is required, but a blank response, say an empty
style sheet, could be sent in place of the typical no-content 204 response.
function sendRequest(url,payload)
{
var linkedStyle = document.createElement("link");
linkedStyle.rel = "stylesheet";
linkedStyle.type = "text/css";
linkedStyle.href = url+"?"+payload;
/* find the head to insert properly */
var head = document.getElementsByTagName("head");
if (head)
head[0].appendChild(linkedStyle);
}
As before, the value of transport that is set in the payload is changed so that the
rating recorded indicates the communication method employed. To run the example online,
visit http://ajaxref.com/ch2/onewaystyle.html.
Location and 204 Response Approach
Given that a browser will stay on the same page when given a 204 response, it can be used
to pretend to go to a URL just to submit some data. To accomplish this, make a direct
assignment with JavaScript to window.location to send the data payload, as shown here:
function sendRequest(url,payload)
{
window.location = url+"?"+payload; // goes nowhere because of 204
}
A 204-based location setting example can be found at http://ajaxref.com/ch2/oneway204.html.
One-way Iframes with Form Posts
One major advantage of using iframes in JavaScript-based communications as compared to
the previous approaches is that iframes also support the HTTP POST method. Given that
query strings used with GET requests have a limited data size, they are inappropriate for