Web学习之----js 操作获取和设置 cookie

引自http://www.nowcoder.com/questionTerminal/eb4d2b3204b14545a78541440c43d969?orderByHotValue=0&done=0&pos=8&mutiTagIds=571&onlyReference=false

// 创建cookie
function setCookie(name, value, expires, path, domain, secure) {
    var cookieText = encodeURIComponent(name) + ‘=‘ + encodeURIComponent(value);
    if (expires instanceof Date) {
        cookieText += ‘; expires=‘ + expires;
    }
    if (path) {
        cookieText += ‘; expires=‘ + expires;
    }
    if (domain) {
        cookieText += ‘; domain=‘ + domain;
    }
    if (secure) {
        cookieText += ‘; secure‘;
    }
    document.cookie = cookieText;
}
// 获取cookie
function getCookie(name) {
    var cookieName = encodeURIComponent(name) + ‘=‘;
    var cookieStart = document.cookie.indexOf(cookieName);
    var cookieValue = null;
    if (cookieStart > -1) {
        var cookieEnd = document.cookie.indexOf(‘;‘, cookieStart);
        if (cookieEnd == -1) {
            cookieEnd = document.cookie.length;
        }
        cookieValue = decodeURIComponent(document.cookie.substring(cookieStart + cookieName.length, cookieEnd));
    }
    return cookieValue;
}
// 删除cookie
function unsetCookie(name) {
    document.cookie = name + "= ; expires=" + new Date(0);
}
时间: 2024-10-11 08:04:41

Web学习之----js 操作获取和设置 cookie的相关文章

js操作获取和设置cookie

//创建cookie function setCookie(name, value, expires, path, domain, secure) {     var cookieText = encodeURIComponent(name) + '=' + encodeURIComponent(value);     if (expires instanceof Date) {         cookieText += '; expires=' + expires;     }     if

js、Java后台设置cookie

概述:小开心下,又学到了点东西 JS设置cookie //添加时间函数 function SetCookie(name, value){ var Days = 30; var exp = new Date(); exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000); document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.

获取和设置cookie的方法(5种)

//--------------------------11111111111 var createCookie = function (a, b, c, d) { var d = d ? d : "/"; if (c) { var e = new Date; e.setTime(e.getTime() + 24 * c * 60 * 60 * 1e3); var f = "; expires=" + e.toGMTString() } else var f = &

今天的学习——关于JS时间获取方面的小问题

①JS获取时间 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-????) myDate.getMonth(); //获取当前月份(0-11,0代表1月) myDate.getDate(); //获取当前日(1-31) myDate.getDay(); //获取当前星期X(0-6,0代表星期天) myDate.getTime(); //获取当前时间(从1

js / jquery 获取和设置 FCK Editor 的值

开发中遇到 通过 $("#content").val(); 或者 document.getElementById("content"); 并不能获取到 id 为 content的值  $("#content").val(--赋值的信息--); 也不能为 id 为 content 赋值: 页面代码: <tr> <td class="p12">内容:</td> <td colspan=&q

纯JS操作获取桌面路径方法

//active 控件获取当前用户的桌面的路径的方法 var wsh = new ActiveXObject("wscript.shell"); listall(wsh.SpecialFolders("Desktop")); function listall(imagePath){ // active 控件读取桌面指定名称图片的方法 var fso=new ActiveXObject("Scripting.FileSystemObject");

获取和设置 cookie——JavaScript代码实现

1 // 创建cookie 2 function setCookie(name, value, expires, path, domain, secure) { 3 var cookieText = encodeURIComponent(name) + '=' +encodeURIComponent(value); 4 if (expires instanceof Date) { 5 cookieText += '; expires=' + expires; 6 } 7 if (path) {

关于tomcat7服务下面js无法获取JSESSIONID的cookie信息

http://tomcat.apache.org/tomcat-5.5-doc/config/context.html Does anyone know what changed in the configuration between Tomcat 6 and Tomcat 7 that would cause the JSESSIONID cookie to not be accessible via JavaScript? Using Tomcat 6: alert(document.co

通过js来设置cookie和读取cookie,实现登陆时记住密码的功能

function setCookie(){ //设置cookie var loginCode = $("#login_code").val(); //获取用户名信息 var pwd = $("#login_password").val(); //获取登陆密码信息 var checked = $("[name='checkbox']:checked");//获取“是否记住密码”复选框 if(checked && checked.le