jason
jason

Reputation: 3615

Google Web App and Cookies Return Undefined

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. As background, I have a Google Sheet, with a Web App attached. That app displays an HTML file.

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?

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

Upvotes: 0

Views: 633

Answers (1)

jason
jason

Reputation: 3615

Well, after some playing around with the code, it seems it has to do with securing the cookie. I am not sure if this is a new requirement (this code worked fine 2 weeks ago), but now, if I create my cookie like this:

document.cookie = "user=John; SameSite=none; secure";

It works fine.

Upvotes: 2

Related Questions