yahya
yahya

Reputation: 91

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?

Upvotes: 0

Views: 964

Answers (1)

amarinediary
amarinediary

Reputation: 5439

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' );

Upvotes: 1

Related Questions