Using Cookies in Google Web App

2,814 views
Skip to first unread message

DreamDelerium

unread,
Aug 17, 2020, 2:34:06 PM8/17/20
to google-apps-sc...@googlegroups.com
Hello,
I wanted to ask if it is possible to use Cookies in a Google Web App?  I believe I had this working a few weeks back, but now when I try to read/write a Cookie, it is undefined.  I have tried using a jquery library, as well as doing this in javascript, but the console always returns a null or undefined response.  In my document load event, I run this code:

       $.cookie("test","some value");
       console.log($.cookie("test"))

and the response is "undefined".  If I try using vanilla javascript, like this:

      document.cookie = "test=some value";
      console.log(document.cookie)

then the output is a blank line. 

Are cookies blocked by Google Web Apps?  Or, am I doing something wrong?

Thanks

EDIT - I was asked to provide a minimal example. This is a basic example that still does not display cookies.


In a Google Sheet, I go to Tools-->Script Editor. Here, I have two files:


Code.js:


function doGet(e) { 
output = HtmlService.createTemplateFromFile('index');
return output.evaluate();
}

index.html:

<!DOCTYPE html> <html> <head> <base target="_top"> <script> document.cookie = "username=John Doe"; var x = document.cookie; console.log( "Hello World" ); console.log( x ); </script> </head> <body> index </body> </html>

When the site loads, only "Hello World" is printed to the console

DreamDelerium

unread,
Aug 18, 2020, 3:02:09 PM8/18/20
to Google Apps Script Community
I am not sure if this a recent change (the way I used cookies two weeks ago worked fine) but I have found that if I better define my cookies as:

document.cookie = "user=John; SameSite=none; secure"; (ensuring the SameSite and Secure options are added)

the cookies work fine.

Martin Hawksey

unread,
Aug 19, 2020, 6:40:28 AM8/19/20
to google-apps-sc...@googlegroups.com
thanks for the update - appears to be a result of a change in the way Chrome handles cookies  https://duo.com/decipher/google-rolls-out-samesite-cookie-changes-to-chrome

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/e1ee64c9-3993-4ec0-a084-7049a87ac719o%40googlegroups.com.


--
Martin Hawksey

Latest tweet (see how):

Google Apps Script is a great tool for programmers of all levels to explore new ideas. @mhawksey shares how Apps Script has removed barriers in education by easing administrative duties, allowing teachers more time to focus on students.

Learn more → https://t.co/HGgCjTgMuK pic.twitter.com/MFwEO9GZV0

— G Suite Developers (@GSuiteDevs) August 10, 2020
Reply all
Reply to author
Forward
0 new messages