获取或设置与javascript的cookie [英] getting or setting cookies with javascript

查看:214
本文介绍了获取或设置与javascript的cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是如果我可以使用javascript设置cookie(并读取)

My question is if I can set a cookie using javascript (and read it)

我的第一印象是下面的代码不工作
如果我在我的vista cookie文件夹,我看不到cookie的名称

My first impression is that the code beneath doesn't work If I look in my vista cookie folder, I can not see the name of the cookie

    function zetCookie(naam,waarde,dagen) {     // setCookie(name,value,days)
    if (dagen) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var verloopdatum = "; expires="+date.toGMTString(); // expiry_date
    }
    else var verloopdatum = "";
    document.cookie = naam+"="+waarde+verloopdatum+"; 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;
}


推荐答案

http://flamencopeko.net/cookie.php ,其中: http://flamencopeko.net/cookie.js 。工作完美。

I'm testing it here: http://flamencopeko.net/cookie.php and here: http://flamencopeko.net/cookie.js. Works perfect.

而且,Firebug的Cookies面板非常棒。

And, yeah, Firebug's Cookies panel is great.

脚本保存字体大小首选项。以下是一个线程:简化字体大小切换

I'm trying to use your script to save font size preference. Here is a thread on that: simplify font size toggle.

这篇关于获取或设置与javascript的cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆