By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,070 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,070 IT Pros & Developers. It's quick & easy.

Query parameters, Javascript and URL decoding issue

P: n/a
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=Ho...&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','y es')",
Server.UrlEncode(title.Replace("'","\\'")).Replace ("+", "%20"),
Server.UrlEncode(category.Replace("'","\\'")).Repl ace("+", "%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=Ho...&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.

Dec 22 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.