2

I am trying to set a session cookie to save an Address. Every time I save the cookie that contains comma followed by space, Safari strips out the space after the commas and breaks the format.

// JavaScript code to save the cookie
document.cookie = "Address=Sample Address, Ontario, Canada;path=/;expire=0;";

// Result
document.cookie => "Address=Sample Address,Ontario,Canada"

Is there any solution to this behaviour? Can we somehow tell Safari not to strip the spaces?

Safari Version 10.1.2 (12603.3.8) | MacOS Sierra Version 10.12.6

Jag
  • 41
  • 2
  • Notice how it preserved your first space but removed all after it. I am seeing behavior more as "only the first space is preserved." This detail is really odd and very safari specific in my testing. – Sql Surfer Apr 25 '18 at 14:36
  • The spaces or equals sign in the payload for Safari is different than all other browsers. I UrlEncoded my cookie payload on set and urldecoded on read. That fixed safari. This next post was Extremely Helpful. https://stackoverflow.com/questions/1969232/allowed-characters-in-cookies?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Sql Surfer Apr 25 '18 at 17:01

1 Answers1

1

The easiest way I found and solved is by UriEncoding the value of the cookie you are trying to set. And decoding the same when you are reading the value of the cookie.

Rishabh Saxena
  • 274
  • 2
  • 6