Jump to content

JavaScript escape function


James_Parsons

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

Link to comment
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
Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...