Creating a URL encoded value in JavaScript

J

James Howe

I'm working on a web application which needs to submit information to the
server via a URL string. The URL can contain values retrieved from form
input fields. I'm using the escape function to preprocess values from
input fields, but I have a problem when the input field contains a '+'
(plus sign). Since the escaped value gets put into a URL, the server
doesn't see the '+', but rather a blank. Is there any other javascript
function which will automatically escape a '+' in addition to the other
characters that it already escapes?

Thanks.
 
M

Michael Winter

I'm working on a web application which needs to submit information to
the server via a URL string. The URL can contain values retrieved from
form input fields. I'm using the escape function to preprocess values
from input fields, but I have a problem when the input field contains a
'+' (plus sign). Since the escaped value gets put into a URL, the
server doesn't see the '+', but rather a blank. Is there any other
javascript function which will automatically escape a '+' in addition
to the other characters that it already escapes?

The simplest thing to do is convert plus symbols to their hex-escaped
versions first, then use the escape() function. When you perform the
reverse, convert plus symbols to spaces, then use unescape().

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top