<% dim ModuleName,InfoID,ChannelShortName,CorrelativeArticle,InstallDir,ChannelDir,Keyword,PageTitle,ArticleIntro,Articlecontent Keyword=stripHTML("程序设计,精通") PageTitle=stripHTML("Quick Tip: one liner cookie read") ArticleIntro=stripHTML("Quick Tip: one liner cookie read,Foralltheobtusenativefunctionsjavascriptoffers(") Articlecontent=stripHTML("

For all the obtuse native functions javascript offers (    wat.blink()
?), …") ModuleName = stripHTML("programme") InfoID = stripHTML("206131") ChannelShortName=stripHTML("编程") InstallDir=stripHTML("http://www.77169.com/") ChannelDir=stripHTML("programme") %> Quick Tip: one liner cookie read - 华盟网 - http://www.77169.com
您现在的位置: 华盟网 >> 编程 >> Java >> 正文

Quick Tip: one liner cookie read

2015/12/8 作者:不祥 来源: 互联网
导读 <% if len(ArticleIntro)<3 then Response.Write Articlecontent 'Response.Write "Articlecontent" else Response.Write ArticleIntro 'Response.Write "ArticleIntro" end if %>

For all the obtuse native functions javascript offers ( "wat".blink() ?), there isn't a simple way to parse cookies and get the value of a single key .

The inter-webs, however, always provide. In my roamings, I have found what is now my favorite getCookie one liner. Here goes:

('; ' + document.cookie) .split('; ' + key + '=') .pop() .split(';') .shift()

Okay, that's not an one-liner, but only for your sake, because we're going to understand that beauty line-by-line.

Let's assume we have the following cookies:

&quot;first=1; doit=2; cookies=3&quot;

Line 1

('; ' + document.cookie)

Nothing special here: we may be searching for the first key, which isn't preceded by ; . Thus, we add it.

Our cookie now looks like this:

&quot;; first=1; doit=2; cookies=3&quot;

Line 2

.split('; ' + key + '=')

From MDN :

The split() method splits a String object into an array of strings by separating the string into substrings. When found, separator is removed from the string and the substrings are returned in an array.

Here, we split the string in two parts, creating an array.

The first part contains everything before our key. We can ignore that.

The second part contains everything after our key. Including the value.

Using the key doit , this is what we would get:

[&quot;; first=1&quot;, &quot;2; cookies=3&quot;] Line 3


From MDN :

The pop method removes the last element from an array and returns that value to the caller

As I promised, we can ignore the first value in that array. Therefore, pop gives us the interesting part:

&quot;2; cookies=3&quot;

Line 4


It's pretty easy from now on, huh? We're getting an array whose first index will contain our desired value. The result:

[&quot;2&quot;, &quot; cookies=3&quot;] Line 5


From MDN :

The shift() method removes the first element from an array and returns that element. This method changes the length of the array.

And for the grand finale, we extract the first value of the array.

Voilà: "2"

That's it.


本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程