mootools
MooTools features the ability to create, read, update, and delete cookies. Lets look at how we can use MooTools to control cookies.
MooTools具有创建,读取,更新和删除cookie的功能。 让我们看看如何使用MooTools来控制Cookie。
Cookie语法 (Cookie Syntax)
The syntax of the cookie, as laid out by the MooTools documentation, is:
根据MooTools文档的规定,cookie的语法为:
var myCookie = Cookie.write(key, value[, options]);
The options include:
选项包括:
domain: the domain for which the cookie will be written
domain :将要写入Cookie的域
duration: the length, in days, that the cookie will last
duration :cookie将持续的长度(以天为单位)
path: specific path the cookie belongs to
path :cookie所属的特定路径
secure: a true/false value specifying if the cookie should be placed as secure
secure :一个true / false值,指定是否应将cookie放置为安全
创建一个cookie (Creating a Cookie)
var cookie = Cookie.write('handle', 'SoccerStar23', {duration: 30, domain: '.davidwalsh.name'}); //save for a month
The above code creates a cookie that will last for 30 days at the root directory on the "davidwalsh.name" domain.
上面的代码创建了一个cookie,该cookie将在“ davidwalsh.name”域上的根目录中持续30天。
更新Cookie (Updating the Cookie)
var cookie = Cookie.write('handle', 'SoccerStar23', {duration: 1, secure: true}); //save for a month
You could delete and re-create the cookie, but why? You can simply overwrite its value and options.
您可以删除并重新创建cookie,但是为什么呢? 您可以简单地覆盖其值和选项。
读取Cookie (Reading a Cookie)
var handle = Cookie.read('handle');
Reading the cookie is just as easy as creating one!
读取cookie就像创建一个cookie一样容易!
删除Cookie (Deleting a Cookie)
if(logoff) { Cookie.dispose('handle'); }
When it's time to remove the cookie manually, the above code works every time.
当需要手动删除cookie时,以上代码每次都会起作用。
So what would you use the MooTools Cookie for? How about saving text-size preferences? Also note that these cookies can be read by PHP too!
那么,您将使用MooTools Cookie做什么? 如何保存文本大小的首选项 ? 还请注意,PHP也可以读取这些cookie!
mootools