Query parameters, Javascript and URL decoding issue

R

R L Vandaveer

I am having an interesting and rather frustrating issue with the
encoding/decoding of parameters with the ASP.NET runtime. What I am
doing isn't exactly revolutionary so hopefully someone has an answer
for this.

I have a page that accepts a number of parameters. Those parameters may
sometimes include URL reserved characters like spaces and ampersands. I
use a combination of HTML and URL encoding to build links to this page
and everything works fine. No rocket science there. When however I open
a new window via javascript, the runtime seems to act unpredicatbly and
misdecode characters, namely %26, the URL encoded ampersand. For
example, the following URL in this case would work like a charm as part
of an href attribute:

mypage.aspx?t=Home%20%26%20Entertaining&c=Home&p=true

You would get three parameter values: "Home & Entertaining", "Home",
and "true". Writing out the Request.Url in the debugger gives the
following absolute URL:

http://mydomain.com/mypage.aspx?t=Home & Entertaining&c=Home&p=true

When that same URL is written into javascript like so:

String.Format("javascript:windowPop('mypage.aspx?t={0}&c={1}&p=true','',515,600,'no','no','yes')",
Server.UrlEncode(title.Replace("'","\\'")).Replace("+", "%20"),
Server.UrlEncode(category.Replace("'","\\'")).Replace("+", "%20"));

*Note that windowPop is just a wrapper for
window.open(url,name,properties) and isn't important. The extra replace
functions are to escape apostrophes properly for c# and Javascript and
get real space encoding %20 instead of the IE specific plus character
UrlEncoding uses. (Causes the URL not to be found in Mozilla based
browsers)

You get four query parameter values: "Home ", " Entertaining", "Home",
and "true". And writing out the absolute URL in the debugger via "?
Request.Url" gives you the following bad URL:

http://mydomain.com/mypage.aspx?t=Home & Entertaining&c=Home&p=true

As you can see the URL encoding for all but the ampersand in the t
parameter is intact.

Anything that can shed light on this behavior would be greatly
apprciated.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top