Jump to content
Sign in to follow this  
James_Parsons

JavaScript escape function

Recommended Posts

I was looking through one of my web dev books when I found a javascript function I just didn't get. it was used in a cookie that stored info about radio buttons chosen. the function is escape() and here is the line

document.cookie="optFont=" +escape(fontChoice)+";expires="+expire.toUTCString();

can anyone tell me what this function does and some common uses

Share this post


Link to post
Share on other sites

escape and unescape shouldn't be used anymore, but there is a description here along with the better alternatives: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Functions?redirectlocale=en-US&redirectslug=Core_JavaScript_1.5_Guide%2FFunctions#escape_and_unescape_functions For the example above, encodeURIComponent would be the function to use instead. https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent

  • Like 1

Share this post


Link to post
Share on other sites

FWIW, some characters have special purposes in the world of cookies and form-type data. The = character is a good illustration. It binds a name to a value. When it does that, it should not be encoded. Routines for extracting data look for the = and use it to parse the data. If the value of the data included an unencoded = character, those routines will attempt to parse a name-value pair that doesn't exist. So now your data is corrupted. That is why we encode the = character. There are similar explanations for other characters that get encoded.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...