P.Doohan
P.Doohan

Reputation: 55

Is there a way to add samesite value to a cookie in jquery?

I have been trying a few variations of syntax to attempt to get the cookie to update with the same site values and appear in chrome devtools like they do for this https://samesite-sandbox.glitch.me/ from chrome.

None of them appear to work, using = instead of : causes errors, changing the case for samesite doesn't seem to function either.

$.cookie("testName", "Test Name",{ expires : 365, SameSite: 'lax'});

I haven't been able to find any information on this online at all. Current Jquery version is 1.12.1

Upvotes: 4

Views: 8912

Answers (2)

Jonathan Harris
Jonathan Harris

Reputation: 433

I have found this to work: append ";SameSite=Lax" to the path.

So, the above example would become:

$.cookie("testName", "Test Name",{ expires : 365, path: "/;SameSite=Lax"});

In $.cookie, the default path is "/". Please note, that as of September 2020, some browsers may also need the "secure: true" appended. Making it:

$.cookie("testName", "Test Name",{ expires : 365, path: "/;SameSite=Lax", secure: true});

Fyi, this method of appending to the path, is used in older frameworks like vbscript or .net 2.0, which don't have some of the more recent settings changes.

Upvotes: 10

Miguel Pérez
Miguel Pérez

Reputation: 101

Looks like the Jquery Cookie Plugin is unmaintained and it's from 2014, reading the docs, it doesn't support that.

So, what are your options?

  1. Fork and modify yourself the plugin to add the functionality.
  2. Switch to https://github.com/js-cookie/js-cookie#samesite (VEry simple and supports samesite directive ofc)

Upvotes: 3

Related Questions