1

It easy to write a fixed string to cookies using such as Cookies.set(\'cookie_2\', \'value\', { expires: 7 }) (see tutorial here).

But how can I save user to cookie_2? Say I have

user = "Klay"
runjs('Cookies.set(\'cookie_2\', user, { expires: 7 });')

Is it possible to achieve this in one-liner? Thanks.

Samoth
  • 716
  • 15
  • 34

2 Answers2

2

Function is runjs('String') so we made the string with our user variable, Like 'String'+variable+'String'

user = "Klay" runjs('Cookies.set(\"cookie_2\",' + user +', { expires: 7 });')

Hafzal BZ
  • 69
  • 7
  • Thanks for the answer, but it did not work. Would you please advice more? Thanks. – Samoth Mar 11 '20 at 08:59
  • `Error in "Cookies.set(\"cookie_2\"," + user : non-numeric argument to binary operator` is the error message. – Samoth Mar 11 '20 at 12:28
0

I finally come up with the answer as follows:

user = "Klay"
string = sprintf("Cookies.set(\'username\', '%s', { expires: 7 });", user)
runjs(string)
Samoth
  • 716
  • 15
  • 34