日期:2014-05-16  浏览次数:20355 次

JS操作Cookie写入和读取的简便方法(一看就懂,直接能用!)!

写入cookie实例!

 var u = "value=123";
  document.cookie = u;

读取cookie实例,读取value的值!

var c_name="value"; // 变量名
   if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=")
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1
            c_end = document.cookie.indexOf(";", c_start)
            if (c_end == -1) c_end = document.cookie.length
            //        document.write(document.cookie.substring(c_start,c_end)+"<br>");
            var sss= unescape(document.cookie.substring(c_start, c_end))  // 取出值 
           alert(sss)
        }
    }