Link to home
Start Free TrialLog in
Avatar of ejiang
ejiang

asked on

Special characters didn't get passed through an querystring

Hi all the experts, I've had an ASP page where it sends a variables to another ASP page through a QueryString, for example, from a click button, I've run this code:
function load(form) {
        var myindex=form.App.selectedIndex;
        var strApp = form.App.options[myindex].text;
        var strURL = "MyPage.asp?App='" + strApp + "'";
        window.alert(strURL);
        parent.view_frame.location.href = strURL;
}
This works fine, I could run MyPage.asp with the QueryString("App") passed over correctly.  However, if the strApp I am passing from above function contains special characters such as: +,# etc., then those special characters are droped off.  And the window.alert showed me that the URL does contains the correct info such as:
MyPage.asp?App=C++ Application
But, if I put a Response.Write on the MyPage.asp, and write out the Request.Query("App")
and it will show me ONLY the "C Application" and two + are dropped.  

Does anyone know what's wrong? Thanks in advance.
Avatar of mattyk
mattyk

You''ll need to make use of Javascript's escape function to encode the URL

so

parent.view_frame.location.href = strURL;


would be

parent.view_frame.location.href = escape(strURL);

-mattyk
Use Server.URLEncode like

MyPage.asp?App=Server.URLEncode("C++ Application")

or

abc = "C++ Application"
MyPage.asp?App=Server.URLEncode(abc)




Avatar of Mark Franz
URLEncode or HTMLEncode?

If you are trying to encode a string, do it before you pass it to the redirect;

var strURL = "MyPage.asp?App='" + "'<%=Server.HTMLEncode(strApp)%>'" + "'";
       

Avatar of ejiang

ASKER

Thanks mattyk, I tried that and it didn't work and I've got an error at line:
parent.view_frame.location.href = escape(strURL);
the error message is "Expected ;"
Remind you that the strURL show me correct result as the C++ does show up there, just when it gets to the MyPage.asp the QueryString("App") doesn't pick up that two pluses.

Thanks jit, that didn't work for me either, after I made it to encode my URL includes the two plus, and when it gets to the MyPage.asp, the QueryString I am getting is still:
'C Application' and the two plus are still dropped, also, if this work, how do I get it Uncoded? Is there a URLUncode function?

Thanks for your quick response.
ASKER CERTIFIED SOLUTION
Avatar of Mark Franz
Mark Franz
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ejiang

ASKER

Thanks mattyk, I tried that and it didn't work and I've got an error at line:
parent.view_frame.location.href = escape(strURL);
the error message is "Expected ;"
Remind you that the strURL show me correct result as the C++ does show up there, just when it gets to the MyPage.asp the QueryString("App") doesn't pick up that two pluses.

Thanks jit, that didn't work for me either, after I made it to encode my URL includes the two plus, and when it gets to the MyPage.asp, the QueryString I am getting is still:
'C Application' and the two plus are still dropped, also, if this work, how do I get it Uncoded? Is there a URLUncode function?

Thanks for your quick response.
Avatar of ejiang

ASKER

Thanks mgfranz, one problem with your solution, I've got this strApp as a client side variable, and you want me to make a conversion using server side script, how that gonna work? If I use
<%= server.HTLMEncode(strApp) %>
it will tell me that Object request, because strApp isn't specified on the server side.  How to get around with this error? Thanks.
Mark took it, I have to retire :-)
Yeah... I thought about this as soon as I posted my comments...  I don't think there is a client-side encoding method in javascript.  Can you run the form objects through a server-side script?
Avatar of ejiang

ASKER

I don't think I would be able to do that, because the selection was made on the client side.  This is a bit strange because it will work perfectly except if the strApp contains a + or # (in my case), it will drop those, and for the case of #, it will cut off anything after that, and for the case of +, it will only drop it and rest of the string still appear.  The most strange thing is that the URL does show up the correct string, ONLY if the ASP is trying to get that passed value such as:
the URL will look like:
MyPage.asp?App=C++ Application
And inside the mypage.asp, if I've got this:
myApp = Request.QueryString("App")
and then myApp will ONLY show "C Application" with two + are being dropped.

I hope this will help to understand exactly what's going on.  Thanks a lot for your help so far.
Are these form values being passed to the same page?  Can you pass them to the MyPage.asp directly instead of going through the JS?
Avatar of ejiang

ASKER

they are different forms, the page where I populate this values is called select_application.asp, and from this page, I then call MyPage.asp and passing in this value.  Any suggestions? I am using W2K as my web server, does that make a difference? Thanks.
<form action=MyPage.asp method=post>
...
<submit>

In Mypage.asp;

myApp = Server.URLEncode(Request.QueryString("App"))




Try using the "\" before the + or the # in your script

So strApp = 'C\+\+ Application'
Might work but not sure !

I have used it for other reasons and it has worked with me.

I don't think that will work enkay, JS or ASP will not escape the characters passed in a URL string.
oops sorry ...  dunno what I was thinking

try this pls

try replacing the # with %23 and the + with %2

That right mgfranz ?

so strApp = 'C%2%2%23Application'
I've noticed that this will be correctly encoded when a form which contains ++ and # is contained as values.  So why not alter your code slightly so that the form that contains your select box is something like

<form action=MyPage.asp target=parent.view_frame>

and then modify your function to be

function load(form) {
   form.submit();
}

-matty
enkay, yes, but we are trying to encode the characters dynamically... I guess you could do a replace() in the JS function to trap all the special chars.
Yuppp... and there are only finite number of characters I hope :)
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
[points to mgfranz]

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

hongjun
EE Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange