将Cookie值设置为去除了UTM参数的url

时间:2019-01-30 02:45:52

标签: javascript cookies

尝试将cookie的值设置为除去查询参数的URL,并将其存储为名为wpLandingPage的cookie。这将需要捕获根URL,无论它是否包含URL参数。我究竟做错了什么?我在“ cookieValue”中没有正确定义变量吗?

<script>
 function getPathFromUrl(url) {
  return url.split("?")[0];
}
 var cookieName = "wpLandingPage"; // Name of your cookie
 var cookieValue = url; // Value of your cookie
 var expirationTime = 2592000; // One month in seconds
 expirationTime = expirationTime * 1000; // Converts expirationtime to milliseconds
 var date = new Date(); 
 var dateTimeNow = date.getTime(); 

 date.setTime(dateTimeNow + expirationTime); // Sets expiration time (Time now + one month)
 var date = date.toUTCString(); // Converts milliseconds to UTC time string
 document.cookie = cookieName+"="+cookieValue+"; expires="+date+"; path=/; domain=." + location.hostname.replace(/^www\./i, ""); // Sets cookie for all subdomains

</script>

0 个答案:

没有答案