You can use the below helper method to create a cookie.
So as an example, to create a cookie that stores the name of a visitor, you can call the helper method like below:
helper.createCookie('username', 'John Doe', 7);
To create a session cookie, don't pass any value for days. So you will be calling the helper method like below:
helper.createCookie('username', 'John Doe');
After executing the code above, the name of the cookie generated is
LSKey[c]username
. The name of the cookie is prefixed with LSKey[<c>]
where c
is the namespace. So to retrieve the value of this cookie, use the below code:So as an example, to retrieve the cookie value, you can call the helper method like below:
helper.getCookie('username');
Check the link in the references section to check how to delete the cookie.
References: https://www.sitepoint.com/how-to-deal-with-cookies-in-javascript/
Doesn't getCookie have to reference document.cookie in some way? It seems like something is missing.
ReplyDeleteWell thanks for this post, but it will be good if you provide with an example like I have a scenario where I have to track who visited the lightning component and how many times.
ReplyDeleteBest Regards,
Naveen Mosuru
how to we create the cookies without the prefix: LSKey[]?
ReplyDeleteIf you disable Locker Service in session management settings to a older version of API, you can disable LSKEY while storing cookies from Lightning pages
ReplyDelete