将cookie值插入隐藏字段

时间:2017-12-06 16:11:55

标签: javascript jquery cookies textfield html-form

我无法获取两个Cookie值并将其添加到我网站上的表单字段中。下面是我添加的脚本,它还填充了JavsScript控制台中的值;但是我无法将cookie值输入表单字段。

表单字段:

<input type="text" id="usource" name="usource" value="">
<input type="text" id="referrer" name="referrer" value="">

我已经尝试过这个JS来填充“usource”和“referrer”字段,但它似乎不起作用。我错过了什么吗?

document.getElementById("usource").value = utmCookie.utm_source;
document.getElementById("referrer").value = utmCookie.referrer;

这是我正在使用的脚本,只要URL中有任何UTM参数,就会在cookie中保存UTM参数。它还将初始引用者信息保存在cookie中,该cookie一直被(365天)覆盖。

var utmCookie = {
 
 
  cookieNamePrefix: "",
 
  utmParams: [
  "utm_source",
  "utm_medium",
  "utm_campaign",
  "utm_term",
  "utm_content"
  ],
 
  cookieExpiryDays: 365,
 
  // From http://www.quirksmode.org/js/cookies.html
  createCookie: function (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 = this.cookieNamePrefix + name + "=" + value + expires + "; domain=.mywebsite.com; path=/";
  },
 
  readCookie: function (name) {
  var nameEQ = this.cookieNamePrefix + 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;
  },
 
  eraseCookie: function (name) {
  this.createCookie(name, "", -1);
  },
 
  getParameterByName: function (name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  console.log(name);
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if (results == null) {
  return "";
  } else {
  return decodeURIComponent(results[1].replace(/\+/g, " "));
  }
  },
 
  utmPresentInUrl: function () {
  var present = false;
  for (var i = 0; i < this.utmParams.length; i++) {
  var param = this.utmParams[i];
  var value = this.getParameterByName(param);
  console.log(param + ' = ' + value);
  if (value != "" && value != undefined) {
  present = true;
  }
  }
  return present;
  },
 
  writeUtmCookieFromParams: function () {
  for (var i = 0; i < this.utmParams.length; i++) {
  var param = this.utmParams[i];
  var value = this.getParameterByName(param);
  this.writeCookieOnce(param, value)
  }
  },
 
  writeCookieOnce: function (name, value) {
  var existingValue = this.readCookie(name);
  if (!existingValue) {
  this.createCookie(name, value, this.cookieExpiryDays);
  }
  },
 
  writeReferrerOnce: function () {
  value = document.referrer;
  if (value === "" || value === undefined) {
  this.writeCookieOnce("referrer", "direct");
  } else {
  this.writeCookieOnce("referrer", value);
  }
  },
 
  referrer: function () {
  return this.readCookie("referrer");
  }
};
 
utmCookie.writeReferrerOnce();
 
if (utmCookie.utmPresentInUrl()) {
  utmCookie.writeUtmCookieFromParams();
}

0 个答案:

没有答案