Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Replacing special characters in query string in javascript


  • From: nick at nickfitz.co.uk (Nick Fitzsimons)
  • Subject: [Javascript] Replacing special characters in query string in javascript
  • Date: Wed Apr 4 08:45:47 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/