Question

I have some javascript that reads the user input from an HTML field (using .text() ). I am then sending that text via URL (FMP protocol, not HTTP, but does the same encoding).

When I enter in multiple spaces (adjacent to each other) I get an apparently rather odd URL encoding result. So, here are some examples with the resulting encoding (one with one space, one with two spaces):

 Bob Bob    =>  Bob%20Bob
 Bob  Bob   =>  Bob%20%c2%a0Bob

%C2 is an 'A' with a symbol over it, while %A0 appears to be a blank?

The problem lies in that on the receiving end of this it doesn't decode correctly as two spaces, but as some other strange characters. Here's some sample code:

$('.newEntry').on('blur', function() {       
     var text = encodeURIComponent ( $('.newEntry').text() ) ;

      if ( currentComment != text ) {  
          window.location = 'fmp://blar.com/Sol?script=AddComment&$commentID=new&$commentText=' + text ;
     }         
 });

Can someone help explain while double spaces are coming through this way? It is HIGHLY probable that we will get double spaces as this is a free form text entry by the user, and they are entering notes to be read by other people.

Thanks, J

Was it helpful?

Solution

According to https://en.wikipedia.org/wiki/Percent-encoding,

The generic URI syntax mandates that new URI schemes that provide for the representation of character data in a URI must, in effect, represent characters from the unreserved set without translation, and should convert all other characters to bytes according to UTF-8, and then percent-encode those values.

Since you have not used a space " " but a nonbreaking-space " " (\u00a0) it is only natural that it got encoded as two bytes, %c2%a0.

The problem lies in that on the receiving end of this it doesn't decode correctly as two spaces, but as some other strange characters.

Yup. Fix the decoding.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top