[Javascript] Replacing special characters in query string in javascript

Nick Fitzsimons nick at nickfitz.co.uk
Wed Apr 4 07:45:26 CDT 2007


On 4 Apr 2007, at 12:14:58, Rajaram Sahasranaman wrote:

> I have a peculiar problem in my application. I have a query string  
> in javascript which I have to pass the Team Name to the next page.  
> Everything is working fine, but suddenly there is a problem occurs  
> now. That is, if there is any ampersand character in the team name,  
> it is not taking the characters after that.
>
> For eg: the team name can be Printing & Stationery. So the query  
> string will take upto Printing and avoid after that. How to add  
> these special character along with he query string in javascript?

The ampersand is one of a number of reserved characters within the  
query component of a URI, as described in section 3.4 of RFC 2396  
<http://www.ietf.org/rfc/rfc2396.txt>.

To correctly encode your query string data, use the  
encodeURIComponent function:

var queryComponent = "Printing & Stationery";
alert(encodeURIComponent(queryComponent)); // shows "Printing%20%26% 
20Stationery"

Regards,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/






More information about the Javascript mailing list