Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  21] [ 1]  / answers: 1 / hits: 22838  / 15 Years ago, mon, february 8, 2010, 12:00:00

I am trying to pass a string via HTTP request which has one of the character as % in the URL query string.



url = url + ?q= + str + &block= + block;  // str contains the '%' character


But on the ColdFusion page where I'm sending this information is returning following error:




Element Q is undefined in URL.




How can I encode the % sign in a URL?


More From » javascript

 Answers
39

You should url-encode all the values you are passing as query parameters, but the url-encoding for % is %25



Update: if you're constructing the query parameters in javascript, you probably want to do:



url=url+?q=+encodeURIComponent(str)+&block=+encodeURIComponent(block)


(Updated again with ZeissS' very helpful suggestion to use encodeURIComponent instead of escape. See also http://xkr.us/articles/javascript/encode-compare/)


[#97632] Thursday, February 4, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucillemariselal

Total Points: 108
Total Questions: 97
Total Answers: 119

Location: Thailand
Member since Thu, May 6, 2021
3 Years ago
;