11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>The</strong> HTTP response code 204 ―No Content,‖ which we alluded to earlier, is a very useful, but<br />

not well-known feature of the HTTP protocol. When a server returns a 204 (as opposed to, say,<br />

a 200 ―OK‖ or 404 ―File not found‖), it is a signal to the browser that for some reason the server<br />

doesn‘t have any data to send in response, so the browser should take no action. Even though<br />

the browser makes the request to the server, pointing your browser at a URL that returns a 204<br />

has no visual effect: the browser appears to do nothing.<br />

We can use HTTP 204 response codes to our advantage by making an RPC call via a<br />

<strong>JavaScript</strong> redirect to a URL that returns a 204. To set this up, configure your Web server (or a<br />

cgi script) to return a 204 for a particular URL, for example, setrating.cgi. <strong>The</strong>n write your<br />

<strong>JavaScript</strong> to redirect the browser to this URL with the parameters of the message you wish to<br />

send.<br />

<strong>The</strong> only part of the previous example that needs to be changed to use this technique is the<br />

sendURL() function, which will now redirect instead of loading a fake image:<br />

function sendURL(url)<br />

{<br />

}<br />

window.location = url;<br />

To send an RPC, the <strong>JavaScript</strong> constructs the URL and then redirects the browser to it. When<br />

the server receives the request, it returns a 204, which causes the browser to cancel navigation<br />

since there‘s no page to navigate to. <strong>The</strong> user is none the wiser, though some browsers may<br />

show a slight indication of browser activity hinting at the behind-the-scenes transmission.<br />

When many developers first encounter this technique, they‘re often quite skeptical that it could<br />

actually work in a wide array of browsers. <strong>The</strong> authors have verified that it works in Internet<br />

Explorer 3 and later, Netscape 4 and later, and Mozilla-based browsers, and we wouldn‘t be<br />

surprised to hear if it was nearly universally supported.<br />

One advantage this technique has over image-based techniques is that it is often perceived by<br />

users as less ―scary.‖ Many users rightly worry about ―Web bugs,‖ invisible images placed on<br />

pages that are fetched from third-party servers in order to track browsing habits. Some users<br />

examining the page are probably more likely to feel comfortable with a redirect than an<br />

undercover image fetch.<br />

A disadvantage of this technique is that if the user‘s browser doesn‘t properly implement HTTP,<br />

it could actually navigate to a blank page as a result of the redirect. Though this is extremely<br />

uncommon, some developers may feel it is a compelling reason to use images instead.<br />

Two-Way Communication<br />

Communicating something from the browser to the server without a round trip is a useful<br />

technique, but much more can be accomplished if <strong>JavaScript</strong> can also receive a return value. In<br />

this section, we cover a number of ways to implement two-way communication (true RPC), from<br />

the primitive to the mature. <strong>The</strong> techniques we discuss are not, however, the only mechanism<br />

by which to do RPC. You could use <strong>JavaScript</strong> to pull in other kinds of dynamic content, to<br />

control a Java applet that talks to the server, to drive an ActiveX control that handles<br />

networking, or to leverage proprietary browser enhancements such as IE‘s data source<br />

features.<br />

Images<br />

Since the height and width properties of an Image are automatically filled in by the browser<br />

once an image has been downloaded, your server can return images of varying dimensions to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!