点赞再看,养成习惯本文
GitHub
https://github.com/qq44924588... 上已经收录,更多往期高赞文章的分类,也整理了不少个人文档,和教程资料。欢迎Star和完善,你们面试能够参照考点复习,但愿咱们一块儿有点东西。javascript
Cookie 为 Web 应用程序保存用户相关信息提供了一种有用的方法。例如,当用户访问我们的站点时,能够利用 Cookie 保存用户首选项或其余信息,这样,当用户下次再访问我们的站点时,应用程序就能够检索之前保存的信息。html
Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递。用户每次访问站点时,Web 应用程序均可以读取 Cookie 包含的信息。java
Cookie的出现是为了解决保存用户信息的问题。例如git
Cookie 能在全部网页中记住用户的信息。它以字符串的形式包含信息,并键值对的形式保存的,即key=value
的格式。各个cookie
之间通常是以“;
”分隔。github
username = Daisy Green
cookie
可能被禁用。当用户很是注重我的隐私保护时,他极可能禁用浏览器的cookie功能;cookie
是与浏览器相关的。这意味着即便访问的是同一个页面,不一样浏览器之间所保存的cookie
也是不能互相访问的;cookie
可能被删除。由于每一个cookie
都是硬盘上的一个文件,所以颇有可能被用户删除;cookie
安全性不够高。全部的cookie
都是以纯文本的形式记录于文件中,所以若是要保存用户名密码等信息时,最好事先通过加密处理。服务器以cookie
的形式向访问者的浏览器发送一些数据。若是浏览器容许接受 cookie。 则将其做为纯文本记录存储在访问者的硬盘上。面试
当访问者跳转到另外一个页面时,浏览器会将相同的cookie发送到服务器进行检索。一旦检索到它,您的服务器就知道或记得之前存储了什么。浏览器
Cookie 在HTTP的头部Header
信息中,HTTP Set-Cookie的Header
格式以下:安全
Set-Cookie: name=value; [expires=date]; [path=path]; [domain=domainname]; [secure];
在HTTP代码中一个具体的例子:服务器
<meta http-equiv="set-cookie" content=" cookieName = cookieValue; expires=01-Dec-2006 01:14:26 GMT; path=/" />
从上面的格式能够看出,Cookie由下面几部分组成。微信
Name/Value对
Name/Value
由分号分隔,一个Cookie
最多有20
对,每一个网页最多有一个Cookie
,Value
的长度不超过4K
。对于Value
值,最好用encodeURIComponent
对其编码。
Domain
Domain
域名也是Cookie的一部分,默认状况下,用户访问网页的域名会存放在Cookie中。若是设置了这个Cookie的域名值,那么意味着域名上的全部服务器,而不只是你正在访问的服务器,都能访问这个Cookie
,一般不要这样作。设置域名的格式以下:domain=http://xyz.com
path
设置对于特定的服务器来讲哪一个目录中的网页可访问Cookie,设置path
的格式是:path = /movies
Expires
设置Cookie
存活的时间,默认状况下,用户关闭浏览器则Cookie
自动删除,若是没有设置Cookie
失效的时间,那么用户关闭浏览器时Cookie
也消失。若是设置该项,就能延长Cookie
的生命期。设置时间在JS 中用Date
对象的GMT
形式,格式以下: expires = date.toGMTString()
Secure
取true
或者false
值。若是为true
,那么必须经过https
发送Cookie
。
在JS中,可使用Document
对象的cookie
属性操做cookie
。 JS 能够读取,建立,修改和删除当前网页的cookie
,,来看看具体的骚操做。
JS可使用document.cookie
属性建立cookie
,能够经过如下方式建立cookie
:
document.cookie = "username=Daisy Green";
还能够添加有效日期(UTC 时间)。默认状况下,在浏览器关闭时会删除 cookie:
document.cookie = "username=Daisy Green; expires=Mon, 26 Aug 2019 12:00:00 UTC";
经过 path 参数,能够告诉浏览器 cookie
属于什么路径。默认状况下,cookie
属于当前页。
document.cookie = "username=Daisy Green; expires=Mon, 26 Aug 2019 12:00:00 UTC"; path=/";
经过 JS,能够这样读取 cookie:
var x = document.cookie;
document.cookie
会在一条字符串中返回全部 cookie,好比:cookie1=value; cookie2
事例:
<html> <head> <script type = "text/javascript"> <!-- function ReadCookie() { var allcookies = document.cookie; document.write ("All Cookies : " + allcookies ); // Get all the cookies pairs in an array cookiearray = allcookies.split(';'); // Now take key value pair out of this array for(var i=0; i<cookiearray.length; i++) { name = cookiearray[i].split('=')[0]; value = cookiearray[i].split('=')[1]; document.write ("Key is : " + name + " and Value is : " + value); } } //--> </script> </head> <body> <form name = "myform" action = ""> <p> click the Button to View Result:</p> <input type = "button" value = "Get Cookie" onclick = "ReadCookie()"/> </form> </body> </html>
运行:
经过使用 JS,我们能够像建立 cookie
同样改变它:
document.cookie = "username=Steve Jobs; expires=Sun, 31 Dec 2017 12:00:00 UTC; path=/";
这样旧 cookie
会被覆盖。
事例:
<html> <head> <script type = "text/javascript"> <!-- function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() + 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write ("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form> </body> </html>
运行:
删除 cookie
很是简单,没必要指定 cookie
值:直接把 expires
参数设置为过去的日期便可:
document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
应该定义 cookie
路径以确保删除正确的 cookie
。若是不指定路径,有些浏览器不会让我们删除 cookie
。
事例:
<html> <head> <script type = "text/javascript"> <!-- function WriteCookie() { var now = new Date(); now.setMonth( now.getMonth() - 1 ); cookievalue = escape(document.myform.customer.value) + ";" document.cookie = "name=" + cookievalue; document.cookie = "expires=" + now.toUTCString() + ";" document.write("Setting Cookies : " + "name=" + cookievalue ); } //--> </script> </head> <body> <form name = "myform" action = ""> Enter name: <input type = "text" name = "customer"/> <input type = "button" value = "Set Cookie" onclick = "WriteCookie()"/> </form> </body> </html>
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。
文章每周持续更新,能够微信搜索「 大迁世界 」第一时间阅读和催更(比博客早一到两篇哟),本文 GitHub https://github.com/qq449245884/xiaozhi 已经收录,整理了不少个人文档,欢迎Star和完善,你们面试能够参照考点复习,另外关注公众号,后台回复福利,便可看到福利,你懂的。