JavaScript Does Not Escape "+" And ColdFusion Does Not Like This One Bit

Posted March 6, 2007 at 8:13 AM by Ben Nadel

Tags: ColdFusion, Javascript / DHTML, AJAX

I have been working on a mini AJAX and ColdFusion powered Chat application so that I can help people debug their code in a real-time way. One bug that has popped up early on is that "+" symbols are getting lost. From what I narrowed down, the "+" is not even making it into the database. The error is on the way In, not the way Out.

After some debugging, I found this page on the Javascript Escape() method, which is what I use to build the URL for AJAX submission:

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

It states that the Javascript Escape() method does NOT escape the "+" symbol. Ok, no problem, right? I mean the "+" isn't really a meaningful URL value as far as I know (URL uses the "&" to separate parameters). Wrong! The "+" symbol represents an encoded space " " in a URL. This is something that ought to be escaped by Javascript. I wonder why they made that decision.

Here is a demo. I created a simple page that took one URL parameter with a "+" symbol and then I dump out the URL structure. Here is the URL:


 
 
 

 
URL value with embedded + symbol  
 
 
 

Here is the URL struct:


 
 
 

 
URL with embedded plus symbol removed in struct  
 
 
 

As you can see, the "+" is getting stripped out of the URL message parameter. It looks like, coming from Javascript, I am going to have escape my "+" symbols manually. In order to do this, I am going to have to replace the "+" with its HEX equivalent, "%2B" after I have escaped the other values:

  • // Escape the URL parameters for this AJAX url request.
  • escape(
  • objParams[ strKey ]
  •  
  • // Escape the "+" manually.
  • ).replace(
  • new RegExp( "\\+", "g" ),
  • "%2B"
  • )

Doing this and the "+" symbol work just fine. I am very curious as to why Javascript treats the "+" symbol as something special, not to be escaped (when it clearly has a very special URL representation that shouldn't be confused). Any thoughts?




Reader Comments

Mar 6, 2007 at 8:38 AM // reply »
2 Comments

Instead of escape() try using encodeURIComponent(). That is part of JavaScript 1.5/ECMAScript 3 standard, and escapes special symbols and does utf8 properly. See docs:

http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Functions:encodeURIComponent


Mar 6, 2007 at 8:39 AM // reply »
11,246 Comments

Ha ha, yeah, I just came across that one as well. Looks like I have to brush up on my 1.5 Javascript.


Feb 25, 2008 at 5:16 PM // reply »
47 Comments

Ben, I read your blog regularly and found this on the web when searching for a chat client for a site that i'm working on. I like the idea and will read the post later when I get back. One thing I want to recommend is the form serializer from http://www.massimocorner.com. I have used some of Massimo's stuff in the past and his code is super clean and elegant, you may want to give that a look.

I tried to chat by clicking on the link and for some reason the cursor kept losing focus. I literally had one hand on the mouse and kept clicking back in and the other to type with.

Not sure if you knew about that little bug.

Look forward to talking to you soon!


Mar 3, 2009 at 9:03 PM // reply »
1 Comments

Thanks heaps for this post, I nailed an issue with UTF8 characters in my web system development that had lingered for years.

Testiment to the value of blogging...


Mar 4, 2009 at 8:02 AM // reply »
11,246 Comments

@Julian,

Glad to help out.


Jun 8, 2009 at 7:49 AM // reply »
34 Comments

Ben,

Thanks for the post, finally got Google to give me the result I wanted.

How annoying this is and I came across this due to an email address.


May 30, 2011 at 3:09 AM // reply »
1 Comments

Thanx a ton, Ben and Erki.. u guys saved me hours of embarrasment.. :)


May 30, 2011 at 10:05 PM // reply »
11,246 Comments

@Anirban,

No problem - also, don't forget to check out the follow up post to this which talks about the function, encodeURIComponent():

http://www.bennadel.com/blog/564-JavaScript-encodeURIComponent-Escapes-Symbol.htm

It makes this post less relevant (and your life easier!).


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
May 23, 2013 at 5:19 AM
Ask Ben: Print Part Of A Web Page With jQuery
How to print also the background color of table cells and table lines ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools