简体   繁体   English

需要与此C#代码等效的Javascript将值添加到Cookie

[英]Need the Javascript equivalent of this C# code to add values to a cookie

I need some help creating the necessary Javascript to add values to an existing cookie. 我需要一些帮助来创建必要的Javascript以将值添加到现有的Cookie中。 I know how to do it in c#, but it needs to be done in Javascript in this scenario. 我知道如何在c#中执行此操作,但是在这种情况下,需要在Javascript中执行。

This is the C# Code: 这是C#代码:

HttpCookie myCookie = HttpContext.Current.Request.Cookies["SiteSettings"];
myCookie.Values.Add(varName, varValue);
myCookie.Expires = DateTime.Now.AddYears(1);
HttpContext.Current.Response.Cookies.Add(myCookie);

Can anyone help me convert this to Javascript? 谁能帮我将其转换为Javascript? I've searched high and low on the internet, and most tutorials don't have a way of specifying the cookie (this site has more than one cookie). 我在互联网上搜索过很多东西,大多数教程都没有指定cookie的方法(此站点有多个cookie)。

Thank you very much, Andrew 非常感谢你,安德鲁

Not sure where you've looked, since the first hit on google for "javascript cookies" is this excellent article by ppk: http://www.quirksmode.org/js/cookies.html 不确定您去过哪里,因为自从Google在Google上首次搜索“ javascript cookie”以来,这是ppk撰写的出色文章: http ://www.quirksmode.org/js/cookies.html

It should answer your question and explain other JS-related cookie-nuances, including example functions for handling cookies in a more sensible way than string concatenation. 它应该回答您的问题并解释其他与JS相关的cookie细微差别,包括用于以比字符串连接更明智的方式处理cookie的示例函数。

I believe: 我相信:

document.cookie= varName + "=" + varValue + ";expires=" + new Date().toUTCString;

That, however, sets the expiration time to now. 但是,这会将到期时间设置为现在。 I don't know how to add a year to it. 我不知道如何增加一年。

There's some good info on javascript handling of cookies here: http://www.quirksmode.org/js/cookies.html 这里有一些关于cookie的javascript处理的好信息: http : //www.quirksmode.org/js/cookies.html

// from your example

var myCookie = readCookie("SiteSettings");
createCookie(varName, varValue, 365);

// from http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

I suggest that you take a look at this jQuery plugin: http://plugins.jquery.com/project/cookie . 我建议您看一下这个jQuery插件: http : //plugins.jquery.com/project/cookie Makes cookie manipulation very easy and it is cross browser safe. 使cookie操作变得非常容易,并且它是跨浏览器安全的。

This will work and will give the desired result 这将起作用并将给出期望的结果

var d = new Date();

document.cookie= varName + "=" + varValue + ";expires=" 
    + new Date((d.getFullYear()+1), d.getMonth(), d.getUTCDate()).toUTCString());

see the article 看文章

Javascript - Cookies Javascript-Cookies

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

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