I have a javascript function which takes a string as its parameter. This string is encoded (with %20 for spaces, %26 for ampersands, etc..).
function myFunction(theParam) {
alert(theParam); // outputs &
}
// called by the following link
<a href="#" onclick='myFunction("%26")'>Do something</a>
How do I stop this behavior? I want myFunction to receive %26 as the parameter and not the ampersand……
Your example alerts
%26as expected for me. (And then falls through to navigating to#. Remember toreturn falsefrom a click handler to stop the link being followed.)You would get an ampersand if you did it in a
javascript:link:as
javascript:URLs are still URLs and undergo normal URL-escaping rules. Of course, you should never use ajavascript:URL anyway.Better, assign from JavaScript itself so you don’t have to worry about HTML-escaping issues either: