encodeURIComponent vs. escape

D

Danny Vucinec

Hi,

I've a problem reading querystring parameters that are 'uri encoded'. Anyone
has a solution for this?

To reproduce the problem, create a classic ASP containing the following code:

-----------------------

<p>Value sent via querystring: <%=
Server.HTMLEncode(Request.QueryString("value")) %></p>
<input id="input" />
<button id="submit" onclick="window.location.assign('Test.asp?value=' +
encodeURIComponent(input.value));">submit</button>
 
A

Anthony Jones

Danny Vucinec said:
Hi,

I've a problem reading querystring parameters that are 'uri encoded'. Anyone
has a solution for this?

To reproduce the problem, create a classic ASP containing the following code:

-----------------------

<p>Value sent via querystring: <%=
Server.HTMLEncode(Request.QueryString("value")) %></p>
<input id="input" />
<button id="submit" onclick="window.location.assign('Test.asp?value=' +
encodeURIComponent(input.value));">submit</button>

-----------------------

Now, enter the value "Dré" and submit. The page will show the value "Dré",
but I except Request.QueryString("value") to return the original value, i.e.
"Dré".

encodeURIComponent correctly encodes using UTF-8 character codes.

Unfortunately IE would incorrectly allow the query portion of a URL to be
sent using whatever local encoding is being used. IIS 5 and 6
correspondingly expect the query portion to be encoded to match the sessions
codepage.

Hence using encodeURIComponent to encode a string into the query generates a
correctly encoded URL which IIS mis-reads.

Use Response.Codepage = 65001 to allow ASP to correctly read the URL.

However unless you reset Response.Codepage to it's original value before
sending any output you will need to ensure you set Response.CharSet =
"UTF-8" and save the page as UTF-8. You will also need to ensure any pages
receiving form posts from the page also have Codepage set to 65001 before
attempting to read the form values.
 
D

Danny Vucinec

This is somewhat complicated in my web application. I found the following
solution for pages using encode/decodeURIComponent:

function decodeURIComponent(text)
{
return unescape(text);
}

function encodeURIComponent(text)
{
return escape(text).replace(/\+/, "%2B");
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top