Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encoding/decoding URL

Status
Not open for further replies.

nkamp

Programmer
Jan 19, 2004
35
NL
Hello,

I have in the URL variabele the "&" character. The browser thinks it's new variabele. What is the best to encode this
Code:
. Write "ExtraString='CompName=DAP Noord & OOST&Var2=xxxxxxx&var3=etc; "
.Write "ExtraString = ExtraString.replace(/\&/g, ""%26"");"
.Write "openModalWin'info_popup_print_antemortem.asp?'+ExtraString

I see on the opened document info_popup_print_antemortem.asp that all the & char are replaced by %26.

Must I decode the URL on the opened document site? But how can I do this without changing the rest of my code like Request("CompName").

Thanks in advance.

Nico
 
Your first line & third line seem strnage at various places (like "dot space write", unmatched apostrophs, non-use of parentheses for a function call etc...) I suppose they are just typos.

Try this with info on query string writing up? (Supposing within "with response ... end with" block...)
[tt]
[highlight].W[/highlight]rite "ExtraString=[red]escape([/red]'CompName=DAP[blue]+[/blue]Noord[blue]+[/blue]&[blue]+[/blue]OOST'[red])[/red] + '&Var2=xxxxxxx&var3=etc';" [blue]& vbcrlf[/blue]
[red]//[/red].Write "ExtraString = ExtraString.replace(/\&/g, ""%26"");"
.Write "openModalWin[red]([/red]'info_popup_print_antemortem.asp?'+ExtraString[red]);[/red]" [blue]& vbcrlf[/blue]
[/tt]
(Here escape would take care of replace of data's "&". Space is encoded as "+" is written in before using escape() function client-side.)
 
Hello tsuji,

That's right there are some typo mistakes. But the thing is that what you're doing is good but then I have to do this for every URL variable and this is in some cases maybe more than twenty.
What my problem is and my question as wel if I say:
Code:
.Write "ExtraString = encodeURIComponent(ExtraString);" & vbCrLf 
['code]
All my & for my URL variable are substituted in %26 (which is good) but then on the print file asp I don't see any variable anymore.

So what I'am asking (or looking for) is there a more convenient/consistent sollution?

Thanks in advance.

Nico
 
So what your looking for is a function that would see a string that looked like this:
Code:
someurl.asp?a=1&b=something&c=3&d=another&field&e=onemore
and would somehow determine it only needs to escape the & in "another&field"?

You might be able to do it with a regular expression by replacing /(=[^&=]*)&([^&=]*&)/g with $1%26$2 or maybe another regular expression using forward lookups. Basically this would be looking for a single occurrence of an ampersand that is inside a string between an equals sign and another ampersand. It would fail to find strings where the ampersand showed up in the field name rather than the value. I think it will also fail on strings with multiple ampersands as well, replacing only the first ampersand in a value.

There is no way that I can think of to make the code recognize an ampersand in the field name (without it knowing what fieldnames to expect, that is). Example:
Code:
?a=b&c&d=e
Could be
a=b
c
d=e

or
a=b
c&d=e

If you will not have a situation where there is an ampersand in the field name, then you could probably use a lookahead regular expression to replace all ampersands that fall between an equals sign and another ampersand:
Code:
re = /&(?=[^&=]*&)/g
myString = myString.replace(re,"%26")

Now what this won't do is replace any of the other characters that normally get encoded, such as =,%,$, ,etc. But if all your concerned about s the ampersand it should do the trick.

-T

barcode_1.gif
 
Mmmmm..

I may be missing the point of what your issue is, but as I understand it:

1. you are writing javascript dynamically from ASP.
2. this code opens a new window with your page
3. it passes several values (or even urls) to the page as parameters
4. it seems the ampersands in the text of some of the inputs is making ASP think it is a new parameter, messing up the extraction from the querystring

so...

would you not just want to use the URLEncode feature of asp to prepare the string so it can be passed as a paramter without messing things up?

Server.URLEncode(sStringInput) etc

so in your example:

Code:
. Write "ExtraString='CompName=DAP Noord & OOST&Var2=xxxxxxx&var3=etc; "

it would be:
Code:
.Write "ExtraString='CompName=" & Server.URLEncode("DAP Noord & OOST") & "&Var2=" & Server.URLEncode("xxxxxxx") & "&var3=" & Server.URLEncode("etc") & ";"

if the values come from a database or a variable, just use that variable in the urlencode function.

Again, maybe I'm missing the point of your issue. But on face value, that seems to be your problem.

A smile is worth a thousand kind words. So smile, it's easy! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top