KeithRules
KeithRules

Reputation: 207

Escape apostrophe in a string saved in a cookie

I need to save a string value in a cookie, and that string (a person's last name) may contain an apostrophe, like O'Bama.

I tried lastName.replace(/'/, "\'").toString(); but I get undefined in a cookie.

What am I doing wrong, and how should this be done correctly?

Upvotes: 0

Views: 1022

Answers (2)

Atrox111
Atrox111

Reputation: 527

Use the escape() function in javascript:

lastname = escape(lastname);

To undo this operation just call unescape()...

This will encode all special chars to store them in your cookie.

Some reference: http://www.w3schools.com/jsref/jsref_escape.asp

Upvotes: 1

arifnpm
arifnpm

Reputation: 417

you only need to escape the string using javascript function:

escape()

and unescape to get the actual value

unescape()

Upvotes: 1

Related Questions