23.04.2013 Views

javascript

javascript

javascript

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 10 ■ SCRIPTING BOM<br />

402<br />

}<br />

if (i !== -1) {<br />

}<br />

Now that we are sure there is a cookie named with the string in the name parameter, we want to clip<br />

its value from the ticker tape. That cut will begin at the index equivalent to i, plus the character length of<br />

the string in name, plus 1 for the = sign. Remember from Chapter 2 that String.length contains the<br />

number of characters comprising a string. So, for a cookie named skin, that would be 4. Let’s assign the<br />

value of the expression indicating the offset where the value of our cookie begins to firstCut:<br />

function getCookie(name) {<br />

var batch = document.cookie, i, firstCut, secondCut;<br />

i = batch.indexOf(name + "=");<br />

if (i !== -1) {<br />

firstCut = i + name.length + 1;<br />

}<br />

}<br />

As you might imagine, secondCut will be the offset in the document.cookie ticker tape where the<br />

value of our cookie ends. That will either be the first semicolon after firstCut or the end of the string of<br />

cookies. So to find the semicolon, we would again call indexOf() on batch. But this time, we would pass<br />

firstCut as the optional second parameter, which tells JavaScript where to begin its search. Note that i<br />

would work here, too. However, passing secondCut results in a quicker match since it’s closer to the<br />

semicolon than i:<br />

function getCookie(name) {<br />

var batch = document.cookie, i, firstCut, secondCut;<br />

i = batch.indexOf(name + "=");<br />

if (i !== -1) {<br />

firstCut = i + name.length + 1;<br />

secondCut = batch.indexOf(";", firstCut);<br />

}<br />

}<br />

In the event that our cookie is the last one in batch, secondCut will contain -1. If that’s the case, we<br />

want to overwrite -1 with the length of the string of cookies. That is to say, we want to overwrite it with<br />

batch.length:<br />

function getCookie(name) {<br />

var batch = document.cookie, i, firstCut, secondCut;<br />

i = batch.indexOf(name + "=");<br />

if (i !== -1) {<br />

firstCut = i + name.length + 1;<br />

secondCut = batch.indexOf(";", firstCut);<br />

if (secondCut === -1) secondCut = batch.length;<br />

}<br />

}<br />

Now for the moment of truth. Clip the value of our cookie from batch by passing<br />

String.substring() the offsets in firstCut and secondCut. However, to decode any whitespace,<br />

commas, or semicolons in the cookie value, be sure to pass the return value of substring() to<br />

decodeURIComponent(). Note that cookie values may not contain any whitespace, commas, or<br />

semicolons, so it’s always best to clean them out just in case. I’ll remind you of that in a bit when we<br />

write the value of our cookie.<br />

Anyway, getCookie() has done its job at this point, so let’s return the cookie value like so:

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

Saved successfully!

Ooh no, something went wrong!