How to set a simple cookie

To avoid losing all variables upon refresh, I’d like to set a simple cookie.

I tried a simple: document.cookie = “username=Max Brown”;
in a Javascript logic with no input and no output.

but getting an error “Cannot set properties of null (setting ‘cookie’)”

How is this best done in AppGyver?

Just an idea that came to mind, and what about this just for testing:

document.cookie = "name=value";

Where “name” is the name of the cookie and “value” is the value you want to assign to the cookie.

You can also set the expiration date of the cookie and the cookie path through additional options string. For example:

document.cookie = "name=value; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/

This will set the “name” cookie with the value “value” and mark it as expiring on the last day of the year 9999 for all routes in the application."

Yes that’s exactly what my test above is doing. It’s truing to affect “Max Brown” to a cookie “username”. But the browser gives that error that it can’t set properties.

hello, please refer to this answer of mine: CSRF Token Validation - Appgyver to SAP S/4 - #8 by Piotr_Tesny
I hope that helps

Thank you @Piotr_Tesny :slight_smile:
I had read your prior post. It looks like it’s about retrieving a cookie though, not writing it. Does it contain tips on how to write them from Javascript? Or using any other Appgyver method?
Thank you everyone!

Hello, please refer to the official documentation on java script cookies: Document.cookie - Web APIs | MDN;
However, please note that document object is not accessible from the Appgyver inline js module… With Appgyver if you need to retain values between app launches you need to use the storage methods. You may browse this forum for pointers on how to use storage…
I hope that helps;

Thank you @Piotr_Tesny, I appreciate the help :slight_smile:
So the answer is that there is no management of cookies through AppGyver, and it should be done by Storage methods.