0

I want to save cookie one and I want to keep updating it on a site basis, not page wise.

Currently I have 5 headers in my navigation box which is static to all pages and on click of header; I am saving header text to the cookie and on every page selected header retrieving. But the problem is that the cookie is saving page wise not site wise. Each page is creating a new cookie.

Code below,

function setCookie(c_name,value,expdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + expdays);
var c_value=escape(value) + ((expdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}


function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
  {
  c_start = c_value.indexOf(c_name + "=");
  }
if (c_start == -1)
  {
  c_value = null;
  }
else
  {
  c_start = c_value.indexOf("=", c_start) + 1;
  var c_end = c_value.indexOf(";", c_start);
  if (c_end == -1)
    {
    c_end = c_value.length;
    }
  c_value = unescape(c_value.substring(c_start,c_end));
  }
return c_value;
}

 $(function($) {

 var menuClickedOld1 = getCookie("SPLeftNavigation");

 if(menuClickedOld1!=null && menuClickedOld1!=""){
 $('li:contains('+menuClickedOld1+') ul').show();
 alert("from cookie "+menuClickedOld1);
 }
 else{
 alert('No values');
 }

 $('.arrw').click(function() { 
 var lname=$(this).parent().text();// selected header name;
 setCookie("SPLeftNavigation",lnName,1);
   });

 });

How can I resolve this problem I already spent a day on it... as it's for production.

Thanks!!

4

1 回答 1

0

哈哈,这真的很有趣。

实际上,cookie 默认情况下以页面方式存储,如果您想将其保存在特定站点或域上,请按如下方式使用:

'SPCookie=myheader; expires=Sat, 7 Sept 2013 20:47:11 UTC; path=/'

完毕 !

于 2013-09-06T12:04:31.317 回答