cookie未按预期设置

时间:2015-06-21 01:24:01

标签: javascript cookies

当我设置这样的cookie时,它可以工作:

var now = new Date();
now.setMinutes(now.getMinutes() + 30); 
document.cookie='bla=cats; expires=' + now + ';path=/;'

但是,当我这样做时,它不会:

  <%= f.simple_fields_for :scope do |finalises_s| %>
      <%= finalises_s.simple_fields_for :finalise do |dr| %>

          <div class="row">
            <div class="col-md-3 col-md-offset-1">

              <%= dr.label :draft, 'Would you like to save this draft and finish it later?', class: 'question-project' %>
            </div>
            <div class="col-md-7">
              <%= dr.collection_radio_buttons :draft, [[true, ' Yes, save a draft'], [false, ' No, publish it']], :first, :last, {:item_wrapper_class => 'fixradio'}, {:class => "response-project"} %>
            </div>
          </div>
          <div id="reminder">
            <div class="row">
              <div class="col-md-3 col-md-offset-1">

                <%= dr.label :reminder, 'Would you like a reminder to complete this draft?', class: 'question-project' %>
              </div>
              <div class="col-md-7">
                <%= dr.collection_radio_buttons :reminder, [[true, ' Yes'], [false, ' No']], :first, :last, {:item_wrapper_class => 'fixradio'}, {:class => "response-project"} %>
              </div>
            </div>

所以我想设置一个30分钟到期的cookie,而不是30天。 30天工作正常,后者没有。为什么?如何设置30分钟的cookie?

2 个答案:

答案 0 :(得分:1)

这将有效:

var now = new Date();
now.setTime(now.getTime() + (30 * 60 * 1000));
document.cookie='bla=cats; expires=' + now + ';path=/;'

答案 1 :(得分:1)

您可以尝试:

now.setTime(now.getTime() + (30*60*1000));

和/或之后通过

将Date对象转换为时间字符串
var expires = now.toGMTString();