Tuesday, March 06, 2012

Escape plus(+) sign in url

Escape plus(+) sign in URL

When we pass some special characters to server side(in URL) for processing we prefer to use the JavaScript function escape(). The work of escape() is to convert the special characters into corresponding ASCII characters.  The server side code automatically does the conversion from ASCII to the original characters.

       But when we pass a plus(+) sign in the escape() function it doesn't converts the plus(+) sign to the corresponding ASCII character. Rather it replaces the plus (+) sign with a space character. Which cause many issue during the AJAX call to the server side.

Also, if we are thinking to avoid escape() function and pass the string directly to then also it will not work.

  When we are escaping a string before escaping that we will convert all the plus(+) sign with the corresponding ASCII character . Then during escape function call it will work like the normal way.

var  strQueryString = "SELECT 1+1 FROM EMPLOYEES";
  1. escape(strQueryString).replace(new RegExp( "\\+", "g" ),"%2B") (will convert plus sign to ASCII)
  2. escape(strQueryString) (will not convert plus sign to ASCII)

You can simply see the difference by the above  code.


  1. I don't know if i am in right track.
    Just want to inform you that when i am making use of escape('somepage.cfm?param=sanu+photo')i am getting "somepage.cfm%3Fparam%3Dsanu+photo" this as output in w3school javascript editor.Are we missing something?

    1. This case is applicable while making some AJAX call.
      To test it just make one AJAX call to some test page and pass a string which contains '+' in the data of the AJAX request then see the value of the parameter actually passed to the server using firebug.

    2. Yes, I think your output string contains '+' sign after applying escape function. So it will create issue when we will try to pass the resulted string in a get request to a server as a URL variable.