检查是否设置了cookie

时间:2018-06-12 08:22:47

标签: javascript jquery

当用户进入网站时,我会弹出一个框,提交时会创建并提交Cookie,但我想要的是当用户刷新页面时显示 - 无框如果已经有cookie设置它:

<div class="box">
   <form action ="" method = "">
   <input type = "checkbox" name = "cookie1" value = "checked1" id="c1" checked>facebook
   <input type = "checkbox" name = "cookie2" value = "checked2" id="c2" checked>google
  <input type = "submit" value = "submit" id="btn">
 </form>
</div>

$(document).ready(function(){
  $("#btn").click(function(){
    function create_cookie(name, value, days2expire, path) {
     var date = new Date();
     date.setTime(date.getTime() + (days2expire * 24 * 60 * 60 * 1000));
     var expires = date.toUTCString();
     document.cookie = name + '=' + value + ';' +
                 'expires=' + expires + ';' +
                 'path=' + path + ';';
 }
  var cookie_name = 'facebook';
  var cookie_value = 'check1';
  create_cookie(cookie_name, cookie_value, 30, "/");
  var cookie_name2 = 'google';
  var cookie_value2 = 'check2';
  create_cookie(cookie_name2, cookie_value2, 30, "/");
})
});

当用户再次进入网站时提交,检查是否已设置cookie,并且不再显示再次设置cookie的弹出窗口。

0 个答案:

没有答案