Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery cookie set value to boolean true

I am using this jquery.cookie plugin and I need to set value to TRUE or NULL/FALSE.

I am trying to do it like this: $.cookie('ff', true, { expires: 30, path: '/' }); but it sets the value to string and not boolean.

Any ways of fixing this?

like image 468
Alex Avatar asked Mar 17 '12 18:03

Alex


People also ask

Can cookie value be boolean?

Cookies are only string-valued. As gdoron commented, if you want to treat the value as a boolean, you need to parse it back to a boolean when the cookie value is read back out.

How do you use cookie values?

Domain: Specifies the domain name of the website. Name=Value: Cookies are stored in the form of name-value pairs. Path: Specifies the webpage or directory that sets the cookie. Secure: Specifies whether the cookie can be retrieved by any server (secure or non-secure).

Is cookie value a string?

Cookies are strings. You'll need to convert the cookie value to the type you want. The boolean values are being saved as true or false because that's the string representation of a boolean.

How do you get data from cookies in JS?

Function explained:Take the cookiename as parameter (cname). Create a variable (name) with the text to search for (cname + "="). Split document.cookie on semicolons into an array called ca (ca = decodedCookie.split(';')). Loop through the ca array (i = 0; i < ca.length; i++), and read out each value c = ca[i]).


1 Answers

Cookies are only string-valued. As gdoron commented, if you want to treat the value as a boolean, you need to parse it back to a boolean when the cookie value is read back out.

Since you commented that you are reading the cookie value with PHP, see Parsing a string into a boolean value in PHP.

like image 80
Matt Ball Avatar answered Sep 21 '22 17:09

Matt Ball