`
ericwzc
  • 浏览: 12553 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Javascript cookie

阅读更多

Cookies
Cookies were originally invented by Netscape to give 'memory' to web servers and browsers. The HTTP
protocol, which arranges for the transfer of web pages to your browser and browser requests for
pages to servers, is state-less
, which means that once the server has sent a page to a browser
requesting it, it doesn't remember a thing about it. So if you come to the same web page a
second, third, hundredth or millionth time, the server once again considers it the very first time you ever came there.

This can be annoying in a number of ways. The server cannot remember if you identified
yourself when you want to access protected pages, it cannot remember your user preferences, it
cannot remember anything. As soon as personalization was invented, this became a major problem.

Cookies were invented to solve this problem. There are other ways to solve it, but cookies are
easy to maintain and very versatile.

How cookies work
A cookie is nothing but a small text file that's stored in your browser. It contains some data:

  1. A name-value pair containing the actual data
  2. An expiry date after which it is no longer valid
  3. The domain and path of the server it should be sent to


As soon as you request a page from a server to which a cookie should be sent, the cookie
is added to the HTTP header. Server side programs can then read out the information
and decide that you have the right to view the page you requested or that you want your links
to be yellow on a green background.

So every time you visit the site the cookie comes from, information about you is available.
This is very nice sometimes, at other times it may somewhat endanger your privacy. Fortunately
more and more browsers give you the opportunity to manage your cookies (deleting the one
from the big ad site, for example).

Cookies can be read by JavaScript too. They're mostly used for storing user preferences.

name-value
Each cookie has a name-value pair
that contains the actual information. The name
of the cookie is for your benefit, you will search for this name when reading out the cookie
information.

If you want to read out the cookie you search for the name and see what value is attached to it.
Read out this value. Of course you yourself have to decide which value(s) the cookie can have and
to write the scripts to deal with these value(s).

Expiry date
Each cookie has an expiry date
after which it is trashed. If you don't specify
the expiry date the cookie is trashed when you close the browser. This expiry date should
be in UTC (Greenwich) time.

Domain and path
Each cookie also has a domain
and a path
. The domain tells the browser
to which domain the cookie should be sent. If you don't specify it, it becomes the domain of
the page that sets the cookie, in the case of this page www.quirksmode.org
.

Please note that the purpose of the domain is to allow cookies to cross sub-domains. My cookie
will not be read by search.quirksmode.org because its domain is www.quirksmode.org . When I set the
domain to quirksmode.org, the search sub-domain may also read the cookie.

I cannot set the cookie domain to a domain I'm not in, I cannot make the domain www.microsoft.com .
Only quirksmode.org is allowed, in this case.

The path
gives you the chance to specify a directory where the cookie is active.
So if you want the cookie to be only sent to pages in the directory cgi-bin, set the
path to /cgi-bin
. Usually the path is set to /
, which means the cookie is
valid throughout the entire domain.

This script does so, so the cookies you can set on this page will be sent to any page in the
www.quirksmode.org
domain (though only this page has a script that searches for the
cookies and does something with them).

document.cookie
Cookies can be created, read and erased by JavaScript. They are accessible through the
property document.cookie
. Though you can treat document.cookie as if it's a
string
, it isn't really, and you have only access to
the name-value
pairs.

If I want to set a cookie for this domain with a
name-value pair 'ppkcookie1=testcookie' that expires in seven days from the moment I
write this sentence, I do

document.cookie =
  'ppkcookie1=testcookie; expires=Thu, 2 Aug 2001 20:47:11 UTC; path=/'


    First the name-value pair ('ppkcookie1=testcookie
    ')
  1. then a semicolon and a space

  2. then the expiry date in the correct format ('expires=Thu, 2 Aug 2001 20:47:11 UTC
    ')
  3. again a semicolon and a space

  4. then the path (path=/
    )


This is a very strict syntax, don't change it!
(Of course the script manages these dirty bits for you)


Also, even though it looks like I'm writing this whole string to the string document.cookie,
as soon as I read it out again I only see the name-value pair:

ppkcookie1=testcookie

If I want to set another cookie, I again do

document.cookie =
  'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

The first cookie is not overwritten, as it would when document.cookie
would be
a real string. Instead the second one is added to document.cookie
, so if we read it out we get

ppkcookie1=testcookie; ppkcookie2=another test

If I reset a cookie

document.cookie =
  'ppkcookie2=yet another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

the old cookie is overwritten and document.cookie reads

ppkcookie1=testcookie; ppkcookie2=yet another test

To read out a cookie you have to treat document.cookie as a
string
and search for certain characters
(semicolons, for instance) and for the cookie name. I'll explain how to do it below.

Finally, to remove a cookie, set it with an expiry date before today. The browser sees
that the cookie has expired and removes it.

document.cookie =
  'ppkcookie2=yet another test; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/'


function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name,"",-1);
}

 

分享到:
评论

相关推荐

    Javascript Cookie操作类的封装

    在JavaScript中,Cookie是一种在客户端存储少量数据的机制,它对于网页应用的用户状态管理和临时存储信息具有重要作用。本文将详细讲解如何封装一个Cookie操作类,实现对Cookie的增删改查功能。 首先,理解Cookie的...

    javascript cookie 操作框架 XCookie

    JavaScript Cookie 操作框架 XCookie 是一个用于方便地管理和操作浏览器cookies的工具,它简化了JavaScript中处理cookie的复杂性。在Web开发中,cookies经常被用来存储用户状态、会话信息或其他临时数据,而XCookie...

    jsp源码实例.rar_cookie_javascript cookie_jsp 实例_jsp 搜索_jsp 表单

    在这个名为"jsp源码实例.rar_cookie_javascript cookie_jsp 实例_jsp 搜索_jsp 表单"的压缩包中,包含了五个关于JSP的源码实例,这些实例涵盖了从基本的输出操作到更复杂的cookie管理和搜索引擎功能。以下是这些实例...

    javascript cookie

    这个文件包含了javascript对浏览器cookie文件的设置、读、写、删除四个函数,非常好用。

    javascript cookie用法基础教程(概念,设置,读取及删除)

    总结,JavaScript Cookie 是一个实用的工具,它允许网站在客户端持久地存储少量数据,实现跨页面的数据共享和用户状态管理。通过理解其基本概念、特点以及如何操作,开发者可以有效地利用 Cookie 来优化用户体验。...

    js实现页面与页面之间传值,js利用地址栏URL传值,利用H5 Web 存储 传值,利用JavaScript Cookie存储 传值

    本文将详细介绍JavaScript如何实现页面间传值,包括通过URL、H5 Web Storage(包括localStorage和sessionStorage)以及JavaScript Cookie来传递和存储数据。 首先,我们来看**通过URL传值**。在浏览器的地址栏中,...

    javascript cookie操作

    javascript cookie的操作,set、get、clear

    JavaScript Cookie显示用户上次访问的时间和次数

    要使用 JavaScript 访问和修改 Cookies,开发者需要通过 document.cookie 对象来操作。document.cookie 包含当前域下所有的 Cookies 字符串。由于这个字符串是用分号分隔的键值对形式存储的,因此需要解析这个字符串...

    JavaScript cookie原理及使用实例

    JavaScript Cookie 是一种在客户端存储少量数据的技术,常用于实现用户会话管理、个性化设置等功能。在Web开发中,Cookie起着关键的角色,因为它们能在浏览器和服务器之间传递信息。以下是关于JavaScript Cookie的...

    CookiesjsJavaScript客户端的Cookie操作库

    "CookiejsJavaScript客户端的Cookie操作库" 指的是一种JavaScript库,专门用于在客户端(即用户浏览器)进行Cookie的操作。Cookiejs是这个库的名称,它提供了一组简单易用的API,帮助开发者方便地管理用户的Cookie。...

    javascript cookie 的应用

    javascript操作cookie的三个函数非常方便。验证过的,实践过的。

    javascript cookie解码函数(兼容ff)

    ### JavaScript Cookie解码函数(兼容Firefox) 在Web开发中,Cookie是存储用户信息的一种常见方式。它可以帮助网站记住用户的偏好设置、登录状态等信息。然而,由于Cookie在传输过程中可能会被编码,因此在读取...

    javascript.cookie

    JavaScript Cookie是Web开发中一种常见的数据存储方式,它允许在用户浏览器端存储少量的数据,这些数据可以在用户下次访问同一网站时被读取。Cookie最初由Netscape Navigator浏览器引入,现在已经成为HTTP协议的一...

    JavaScript Cookie的读取和写入函数

    JavaScript Cookie的读取和写入函数 在Web开发中,Cookie是客户端存储数据的一种方式,通过JavaScript可以实现Cookie的读取和写入。本文将详细介绍JavaScript Cookie的读取和写入函数的实现。 Cookie概述 Cookie...

    javascript 获取cookie

    通过函数获取JavaScript cookie

    cookie应用(javascript)

    在JavaScript编程中,Cookie是一种非常重要的技术,用于在客户端存储少量的数据。Cookie的应用广泛,例如用户会话管理、个性化设置、跟踪用户行为等。在这个"cookie应用(javascript)"的压缩包中,包含了一个JS文件和...

    simple-cookie:您将了解的一个非常简单的超轻量级JavaScript cookie类

    简单的Cookie 这是一个非常简单的超轻量级JavaScript cookie类,可根据需要使用。 它有4种简单的方法...设置Cookie Cookie . set ( 'name' , 'value' ) 您也可以使用它来设置到期时间,默认情况下它永不到期。 到期...

    cookie-dough:同构JavaScript cookie库

    曲奇饼干同构JavaScript cookie库。 包装以在客户端和服务器上工作。 此外,还需要快速解析Cookie(目前)。用法获取一个cookie // client-side// automatically parses the cookies for the pagevar cookie = ...

    javascript cookie基础应用之记录用户名的方法

    在 JavaScript 中,我们可以使用内置的 `document.cookie` 属性来读写 Cookie。然而,`document.cookie` 只能用来设置整个字符串,因此在实际操作中,我们通常会封装一些函数来简化操作。 以下是一个简单的 ...

Global site tag (gtag.js) - Google Analytics