简体   繁体   English

无法理解JavaScript代码

[英]Unable to understand a JavaScript code

I am learning to create cookies in JavaScript, I am having problems in understanding the working of last 3 lines of code. 我正在学习在JavaScript中创建cookie,在理解最后三行代码的工作时遇到问题。 I know this Question doesn't suits the Stackoverflow Standand but I will be grateful if anyone kindly explains it. 我知道这个问题不适合Stackoverflow Standand,但如果有人请解释我将不胜感激。

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; 
expires="+exdate.toUTCString()); 
document.cookie=c_name + "=" + c_value;
}

If exdays is not null, ie it is given as an argument (in JavaScript, functions can take any number of arguments), with a ternary check (if/else shorthand) it appends the string "expires=..." to the cookie string. 如果exdays不为null,即以参数形式给出(在JavaScript中,函数可以采用任意数量的参数),并通过三元检查(if / else速记)将字符串“ expires = ...”附加到cookie串。 Else, there is no expires string (it will be a session cookie). 否则,没有到期字符串(它将是会话cookie)。

Finally, document.cookie is modified. 最后,修改document.cookie。 For more info on cookies and changing via JS, see www.quirksmode.org/js/cookies.html 有关Cookie和通过JS进行更改的更多信息,请参见www.quirksmode.org/js/cookies.html

Basically, to add a new cookie using JS, you set document.cookie = "key=value". 基本上,要使用JS添加新的cookie,请设置document.cookie =“ key = value”。 Other cookies are not overwritten, the new cookie is simply appended. 其他cookie不会被覆盖,只需添加新的cookie。

To delete other cookies, one needs to set an expiry date in the past and they will be cleared by the browser. 要删除其他Cookie,您需要设置一个过去的过期日期,浏览器会清除它们。

If you simply print document.cookie, you will see all cookies (technically not all, except http-only cookies etc.), but there is no way to learn their expiry dates from JavaScript. 如果仅打印document.cookie,您将看到所有cookie(技术上不是全部,除了纯HTTP的cookie等),但是无法从JavaScript学习它们的有效期。


Well said by ustun, if you understand the general format for writing and retrieving the cookie then you can be a master in it. ustun说得好,如果您了解编写和检索cookie的通用格式,那么您可以成为其中的高手。 It's just as simple as like handling Strings and arrays. 就像处理字符串和数组一样简单。
Cookies are very useful component for storing the small sized infrequent content. Cookies是用于存储少量不频繁内容的非常有用的组件。 Normally it's used for transferring the small amount of data(like current user name, or the users unique ID & so) from one page to another page or to communicate with a server for specific operations. 通常,它用于将少量数据(例如当前用户名或用户唯一ID等)从一页转移到另一页,或用于与服务器通信以进行特定操作。

Setting The Cookie Setting up the cookie is normally very easy. 设置Cookie设置cookie通常非常容易。 It just require three part in it's definition.That they are, 它的定义只需要三部分。

  • Data to be stored 要存储的数据
  • Expire date for your cookie(Optional), Cookie的过期日期(可选),
  • Domain of your cookie (Optional). Cookie的域(可选)。

    More detailed information about these parameters can be available here . 有关这些参数的更多详细信息,请参见此处
    Example: document.cookie = "name = test ; expires = date ; path =/" 示例: document.cookie = "name = test ; expires = date ; path =/"

    Retrieving The cookie values As i said its very simple like processing the array string. 检索cookie值正如我所说的,它非常简单,就像处理数组字符串一样。 The semicolon(;) in the above example will act as a separator for our stored cookie. 上例中的分号(;)将用作存储的cookie的分隔符。
    Example var myCookie = document.cookie.split(';'); for(var i=0;i < myCookie.length;i++) { var cookieValue = myCookie[i]; var pair = myCookie[i].split('=');var key = pair[0];var value= pair[1]; } 示例 var myCookie = document.cookie.split(';'); for(var i=0;i < myCookie.length;i++) { var cookieValue = myCookie[i]; var pair = myCookie[i].split('=');var key = pair[0];var value= pair[1]; } var myCookie = document.cookie.split(';'); for(var i=0;i < myCookie.length;i++) { var cookieValue = myCookie[i]; var pair = myCookie[i].split('=');var key = pair[0];var value= pair[1]; }

    Limitation Even it's very handy to use but they are not meant as a normal communication or mechanism. 局限性即使使用起来非常方便,但它们并不是正常的通信或机制。 Note that web browsers are not required to save more than 300 cookies total, nor more than 20 cookies per web server (for the entire server, not just for the page or site on the server), nor to retain more than 4 kilobytes of data per cookie (both name and value count towards this 4 kilobyte limit). 请注意,不需要Web浏览器总共保存300个以上的cookie,每个Web服务器上不需要保存20个以上的cookie(对于整个服务器,而不仅仅是服务器上的页面或网站),也不需要保留4 KB以上的数据每个Cookie(名称和值均计入这4 KB的限制)。 The biggest limitation of these is the 20 cookies per server limit, and so it is not a good idea to use a different cookie for each variable that has to be saved. 这些最大的限制是每个服务器限制20个cookie,因此对必须保存的每个变量使用不同的cookie并不是一个好主意。 Rather save a single cookie containing a lot of information. 而是保存一个包含很多信息的cookie。

  • 声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM