If you build all the HTML text with simple steps, some of these problems will go away. Therefore, taking your example, you want to encode the following query parameter:
M&S
When you insert this string as a request parameter in the URL, you must specify it as you already know. String with urlencoded M%26S . Then the full URL is as follows:
http://10.0.0.195/program.exe?Qry147=M%26S&sortmethod1=147
Now this URL is embedded in the JavaScript code, in which case you only need single quotes from both ends:
'http://10.0.0.195/program.exe?Qry147=M%26S&sortmethod1=147'
All JavaScript code is as follows:
AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')
Now all this text is used in the context of HTML, which is interpreted as a URL, so you need urlencode again:
AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')
And finally, since you embed this text in HTML, you need htmlescape of it:
AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')
This is why you end:
<a href="javascript:AJAX_Get('http://10.0.0.195/program.exe?Qry147=M%2526S&sortmethod1=147')" title='Refresh'>Refresh</a>
I usually avoid these coding issues by not putting the M&S directly in the onclick event or anchor. In general, you cannot encode the onclick event and binding in the same way, since the decoding process for both is different:
onclick: html -> js -> url anchor: html -> url -> js -> url
But wait ... if you write such a helper function, it works:
function myQuery(q) { var encodedQ = encodeURIComponent(q);
Now you can write:
<a href="javascript:myQuery('M&S')">anchor</a> <a onclick="myQuery('M&S')">event</a>
This trick works because in the anchor case there is no more % .