We have a menu in a web application that uses <a> tags to load pages into the main frame.
A typical menu item would be something like:
<a target="mainframe" href="/servlet1?param1=val1&parma2=servlet2?s2p1=val2%26s2p2=val3¶m3=val4">Menu Item 1</a>
We needed to add some JavaScript validation before requesting a link, so I changed it to:
<a target="mainframe" href="javascript:validate('/servlet1?param1=val1&parma2=servlet2?s2p1=val2%26s2p2=val3¶m3=val4')">Menu Item 1</a>
(I know that javascript:function in a link is bad practice, but we use a third-party library to create the menu so that I cannot change this part of the code)
Servlet1 expects:
param1 = 'val1'
param2 = 's2p1 = val2% 26s2p2 = val3 servlet2?
param3 = 'val4'
Servlet1 will then go to param2, so Servlet2 expects:
s2p1 = 'value2'
s2p2 = 'val3'
However, when I put alert in my check function to check what was passed:
function validate(href) { alert(href); ...validation code... }
He gives:
/ servlet 1? param1 = val1 & parma2 = servlet2? s2p1 = val2 ** & ** s2p2 = val3 & param3 = val4 (note the bold & , which was %26 in the above function call)
%26 converts to & when it is passed to the JS function, which normally will not be executed until the request is redirected to Servlet2 . Since %26 has already been changed to & , the request parameter s2p2 gets the value servlet1 instead of Servlet2 .
Basically, my question is why %26 converts to & at this point, just passing it as a function parameter from the href attribute if you do onClick="validate('/servlet1?param1=val1&parma2=servlet2?s2p1=val2%26s2p2=val3¶m3=val4')"
it remains as %26 , as you expected?