简体   繁体   English

Cookie在IE中关闭浏览器窗口时被清除

[英]Cookies get cleared on closing browser window in IE

I am using cookie plug in my application. 我在应用程序中使用Cookie插件。 where i display some information from cookies. 我在其中显示Cookie的一些信息。 it is working fine with firefox, but have some issues with IE. 它与firefox正常工作,但IE出现了一些问题。 It works fine if i open a new tab in IE, it shows values from cookies, but if i close IE browser window it clears all cookies. 如果我在IE中打开一个新选项卡,它会很好地工作,它显示cookie中的值,但是如果我关闭IE浏览器窗口,它将清除所有cookie。 this is not the case with mozilla, every time i open mozilla it shows those specific values from cookies. mozilla并非如此,每次我打开mozilla时,它都会显示cookie中的那些特定值。 below is the code i use to set/get cookies. 以下是我用于设置/获取Cookie的代码。

function setCookies(){<br/>      
   if($('select[id$="ddlFromStation"]')[0].selectedIndex!=0){<br/>
      $.cookie('d_from',$('select[id$="ddlFromStation"]').val());<br/>
   }<br/>
   if($('select[id$="ddlToStation"]')[0].selectedIndex!=0){<br/>
      $.cookie('d_to',$('select[id$="ddlToStation"]').val());<br/>
   }<br/>
   if($('input[id$="txtFromStation"]').val()!=""){<br/>
      $.cookie('i_from',$('input[id$="txtFromStation"]').val());<br/>
   }<br/>
   if($('input[id$="txtToStation"]').val()!=""){<br/>
      $.cookie('i_to',$('input[id$="txtToStation"]').val());<br/>
   }<br/>
   return true;<br/>
}<br/>
$(document).ready(function(){<br/>
 if($.cookie('d_from')!=null){<br/>
    $('select[id$="ddlFromStation"]').val($.cookie('d_from'))<br/>
 }<br/>
 if($.cookie('d_to')!=null){<br/>
    $('select[id$="ddlToStation"]').val($.cookie('d_to'))<br/>
 }<br/>
 if($.cookie('i_from')!=null){<br/>
    $('input[id$="txtFromStation"]').val($.cookie('i_from'))<br/>
 }<br/>
 if($.cookie('i_to')!=null){<br/>
    $('input[id$="txtToStation"]').val($.cookie('i_to'))<br/>
 }<br/>
}); <br/>

You are not setting an expiry and by default cookies should expire on browser close. 您没有设置过期时间,默认情况下,cookie应该在浏览器关闭时过期。 I'm not sure why Mozilla isn't doing this which is the real bug. 我不确定为什么Mozilla 没有这样做,这是真正的错误。

Try: 尝试:

$.cookie(COOKIE_NAME, VALUE, { expires: 10 }); /* Expires in 10 days */

Cookies are volatile - depending on the browser settings, they may get cleared when the browser closes. Cookies是易失性的-根据浏览器的设置,关闭浏览器后可能会清除它们。 You can set up Firefox to do this, so it isn't unique to Internet Explorer. 您可以设置Firefox来执行此操作,因此它并不是Internet Explorer独有的。 However, you may find some benefit to setting a long expiry on the cookies to ask the browser to keep them. 但是,您可能会发现对Cookie设置较长的期限以要求浏览器保留它们会有所帮助。

If you want to keep information, you need to do that in a database on your server, as you know it won't be cleared out! 如果您想保留信息,则需要在服务器上的数据库中进行操作,因为您知道不会清除该信息!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM