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.

Note Don’t think that because we are using form buttons here that this is a traditional style<br />

communication. In fact, we could have used just about any object we could click by<br />

attaching an onclick, including links () or even structural elements like or<br />

, but we used form buttons since the user would feel they were clickable!<br />

We‘ve omitted the server-side script that presumably handles these requests. You could write a<br />

simple CGI that returns a 1-pixel-by-1-pixel image if you like, or you could just return any old<br />

content you like. Since we‘re not doing anything with the result, we don‘t even care if the server<br />

returns an error. In fact, you don‘t even have to have a setrating.cgi to handle this request.<br />

Instead, you could just let the server return a 404 and extract the information from your logs.<br />

<strong>The</strong>se requests will result in log lines that look something like this:<br />

www.example.com - - [19/Mar/2004:21:05:29 -0800] "GET<br />

/setrating.cgi?productid=2158&rating=1&user=fritz HTTP/1.0" 403 305<br />

You could easily write a script to comb your logs for these messages, parse them, and do with<br />

the information what you will (e.g., enter it in a database).<br />

Note In the preceding example, you don’t necessarily have to pass user information via the CGI<br />

parameters. If you use cookies for authentication, the cookie will be sent as usual along<br />

with the request, and the server-side script can extract the user’s identity from the cookie.<br />

A more elegant approach would be to have your server-side program record the data and then<br />

actually pass back a proper response code to the browser. In this case, we could return a 204<br />

HTTP Response code indicating no content so that the browser wouldn‘t think anything was<br />

amiss. While this would appear the cleaner way to do things, it isn‘t really necessary. However,<br />

don‘t play fast and loose with HTTP; there are significant pitfalls to avoid.<br />

Encoding Parameters<br />

Some characters are illegal in URLs, and Web servers will often choke if they are included. For<br />

example, you can‘t have carriage returns or backspaces in a URL. If the parameters you wish<br />

to pass the server might include problematic characters, you need to encode them. Encoding<br />

replaces problematic characters with their ASCII values as a hexadecimal escape sequence,<br />

for example, %0D for a new line. <strong>The</strong> Web server automatically decodes URLs it receives and<br />

makes the decoded parameters available to CGIs and the like.<br />

To encode a string, simply call encode() on it. We can rewrite the parameter setup code from<br />

the previous example in this way:<br />

var params = "productid=" + encode(productid);<br />

params += "&rating=" + encode(rating);<br />

params += "&user="<br />

It‘s almost always a good idea to encode your parameters, even if you don‘t think there‘s a<br />

chance for problematic characters to sneak in.<br />

Other Objects<br />

<strong>The</strong>re‘s no particular reason to use Images for this RPC other than that they‘re widely<br />

supported and simple to script. Any (X)HTML element that has a property that can be set to a<br />

URL will work. One common technique is to use a single hidden for<br />

communication. Whenever you wish to send a message to the server, set the ‗s src<br />

to the message URL. We‘ll see that s are more commonly used for two-way<br />

communication in a later section.<br />

Redirects

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

Saved successfully!

Ooh no, something went wrong!