简体   繁体   中英

how can i write a cookie from a page to another page

I have the code below on https://kooboid.com/sign-up/step-two/ . but I want to save cookies on https://kooboid.com/payment too.the cookie will save on https://kooboid.com/sign-up/step-two/ successfully and i can use it. but it does not seem to save cookie on https://kooboid.com/payment . I do not know why.

 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; }

when I print the cookie in a script on https://kooboid.com/payment , with this code:

 console.log(document.cookie);

it does not include the cookie which I set for https://kooboid.com/payment in the code on https://kooboid.com/sign-up/step-two/ . what is the problem?

Just use localStorage, it does the same job, just way easier. I'm assuming you have some knowledge with JS.

Set the item: localStorage.setItem( 'plan', '/sign-up/step-two/' );

Get the item: localStorage.getItem( 'plan' );

Remove item: localStorage.removeItem( 'plan' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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