470,596 Members | 1,609 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,596 developers. It's quick & easy.

encodeURIComponent vs. escape

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é".
Sep 7 '07 #1
2 12050
"Danny Vucinec" <Da**********@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...
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.
--
Anthony Jones - MVP ASP/ASP.NET
Sep 7 '07 #2
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");
}
"Anthony Jones" wrote:
"Danny Vucinec" <Da**********@discussions.microsoft.comwrote in message
news:B8**********************************@microsof t.com...
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.
--
Anthony Jones - MVP ASP/ASP.NET
Sep 13 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Leif B. Kristensen | last post: by
18 posts views Thread by Steve Litvack | last post: by
15 posts views Thread by pkaeowic | last post: by
131 posts views Thread by Lawrence D'Oliveiro | last post: by
9 posts views Thread by Serguei.Goumeniouk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.