Passing an '&' as part of the parameter's value in a query string

  • Thread starter Thread starter moondaddy
  • Start date Start date
M

moondaddy

I have a situation where I will collect text from a textbox and pass that
text as one of the parameters in another pages query string. the problem is
that if the user types in a '&' as part of the text message, the page being
called thinks the '&' is parsing out another parameter. Using JavaScript,
what should I do to pass the entire message in the query string?

Thanks.
 
moondaddy said:
I have a situation where I will collect text from a textbox and pass that
text as one of the parameters in another pages query string. the problem is
that if the user types in a '&' as part of the text message, the page being
called thinks the '&' is parsing out another parameter. Using JavaScript,
what should I do to pass the entire message in the query string?

Thanks.

Use the encodeURIComponent function to encode the value that you put in
the URL.

You should do this for any values that you put in an URL that might
contain characters that needs encoding.
 
I have a situation where I will collect text from a textbox and pass that
text as one of the parameters in another pages query string. the problem is
that if the user types in a '&' as part of the text message, the page being
called thinks the '&' is parsing out another parameter. Using JavaScript,
what should I do to pass the entire message in the query string?

& = %26
 
Alexey said:

That is correct, but that only solves the problem with that specific
character. There are several other characters that are not valid in a
value in a query string, like spaces for example.
 
Thanks!

encodeURIComponent worked perfect.


Göran Andersson said:
Use the encodeURIComponent function to encode the value that you put in
the URL.

You should do this for any values that you put in an URL that might
contain characters that needs encoding.
 
Back
Top