Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb
"Accelerated DOM Scripting with Ajax, APIs, and Libraries"
split('&');
var tmp;
for(var i=0;i
{
tmp = data[i].split('=');
qsObject[tmp[0]] = tmp[1];
}
alert(qsObject.sortBy); // alerts "title"
Keep in mind that if there are duplicate keys, the last one defined in the query string will
take precedence.
You can use a comma-separated value (CSV) as another format for separating a string
into a number of parts. Each record is separated by a new line, and each field is separated by
a comma (hence comma-separated). Parsing a line of CSV isn??™t just a matter of splitting the
string at the commas. Take a look at this example:
"Mr. Smith, Esq.", 2006, January, 26,Need to get in touch
Notice the comma in the first field. The entire field gets wrapped in quotes to indicate
that it??™s all one field. And what if you have quotes inside a field? It gets even more complicated.
What if you have empty records? Yup, even more difficult. String parsing in this fashion is
practical only with simple data sets that are easily separated.
Returning the data in any other format besides JSON usually means parsing the data into
JavaScript before you can manipulate it; hence the reason JSON has become such a popular
format.
CHAPTER 5 n AJAX AND DATA EXCHANGE 116
nCaution As much as you want to think the data coming back to you is reliable, it might not be. Be suspicious
of anything and plan for it accordingly.
Building a Reusable Ajax Object
Now that you have an understanding of Ajax and how its information can be sent back and
forth, let??™s look at creating an object that you can use with the projects.
Pages:
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183