I am encoding a string that will be passed in a url (via get). But, if I use escape
, encodeURI
or encodeURIComponent
, &
will be replaced with %26amp%3B
, but I want it to be replaced with %26
. What am I doing wrong?
|
||||
Without seeing your code, it's hard to answer other than a stab in the dark. I would guess that the string you're passing to encodeURIComponent(), which is the correct method to use, is coming from the result of accessing the innerHTML property. The solution is to get the innerText/textContent property value instead:
If that isn't the case, you can use the replace() method to replace the HTML entity:
|
||||
|
If you did literally this:
Then the result is |
|||||
|
There is HTML and URI encodings. So before URI encoding your string you might want to HTML decode and then URI encode it :)
result Hope this saves you some time |
|||
|
&
is the proper way to escape the ampersand in an HTML context...where is your source coming from? and what's the destination? It may be better to do this server-side for example. – Nick Craver♦ Aug 22 '10 at 13:59