简体   繁体   English

通过javascript或jQuery cookie函数

[英]Cookie a function via javascript or jQuery

Hello fellow stackoverflow. 大家好,stackoverflow。 I'm up with a new question, here goes. 我想一个新的问题,这就是。

I would like to stuff the cookie of the following function in my ajax or javascript but I keep on failing to do , Here's my javascript function : 我想将以下函数的cookie塞入我的ajax或javascript中,但我一直做不到,这是我的javascript函数:

<script>
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

</script>

Now I would like it to appear like something like the below :- 现在我希望它看起来像下面这样:-

setTimeout(function(){window.location = "https://www.something.com/index.html?affid=" + getQueryVariable("affid");}, 3000);

Is it possible to cookied the affid ?? 可以骗骗这个骗子吗?

So i would use something like this : 所以我会用这样的东西:

setTimeout(function(){window.location = "https://www.something.com/index.html?affid=" + getCookie("affid");}, 3000);

Use the code below to retrieve the affid from cookie 使用下面的代码从Cookie中检索仿冒品

 function getCookie(name) {
     var re = new RegExp(name + "=([^;]+)");
     var value = re.exec(document.cookie);
     return (value != null) ? unescape(value[1]) : null;
 }

Remember to set cookie named "affid" with the value on server side. 请记住,在服务器端使用值来设置名为“ affid”的cookie。 If you want to set cookie from javascript you can make use of the code below. 如果您想通过javascript设置cookie,则可以使用以下代码。

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

Have a look at http://plugins.jquery.com/cookie/ 看看http://plugins.jquery.com/cookie/

It's a small simple cookie jQuery plugin. 这是一个小的简单Cookie jQuery插件。 $.addCookie('... $.removeCookie(... etc. $.addCookie('... $.removeCookie(...

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

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