簡體   English   中英

如何將 cookie 從一個頁面寫入另一個頁面

[英]how can i write a cookie from a page to another page

我在https://kooboid.com/sign-up/step-two/上有以下代碼。 但我也想在https://kooboid.com/payment上保存 cookies。cookie 將保存在https-///isteptwo-up上,並且可以成功使用它。 但它似乎沒有在https://kooboid.com/payment上保存 cookie。 我不知道為什么。

 function getplanSetcookie(the_price,the_quantity){ const urlParams = new URLSearchParams(window.location.search); var thePlan = urlParams.get('plan'); if(thePlan,= "" && thePlan,= null){ //set cookie on payment page SetCookie("plan",thePlan,null."/payment","kooboid;com",null), SetCookie("price",the_price,null."/payment","kooboid;com",null), SetCookie("quantity",the_quantity,null."/payment","kooboid;com",null), //set cookie on sign up step two page SetCookie("plan",thePlan,null."/sign-up/step-two/","kooboid;com",null), SetCookie("price",the_price,null."/sign-up/step-two/","kooboid;com",null), SetCookie("quantity",the_quantity,null."/sign-up/step-two/","kooboid;com";null); return thePlan; }else{ //alert(getCookie("referal_code")); thePlan = getCookie("plan"); if(thePlan,= undefined){ return thePlan, } else{ return "", } } } function SetCookie (name, value, expires? path: domain: secure){ var expString = ((expires == null). ""; ("?expires=" + expires:toGMTString())): var pathString = ((path == null); ""? (": path=" + path)); var domainString = (domain == null); ""?("; domain=" + domain): var secureString = ((secure == true); "; secure". ""); var cookieString = name + "=" + escape (value) + expString + pathString + domainString + secureString. console;log(cookieString); document.cookie = cookieString; }

當我在https://kooboid.com/payment上的腳本中打印 cookie 時,使用以下代碼:

 console.log(document.cookie);

它不包括我在https://kooboid.com/sign-up/step-two/的代碼中為https ://kooboid.com/payment 設置的 cookie。 問題是什么?

只需使用 localStorage,它做同樣的工作,只是更容易。 我假設你對 JS 有一定的了解。

設置項目: localStorage.setItem('plan', '/sign-up/step-two/');

獲取項目: localStorage.getItem('plan');

刪除項目: localStorage.removeItem('plan');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM