Forum Moderators: coopster
I have a link like this on a page:
<?php
echo "<A HREF=\"javascript:popup('script.php?",
"var1=", urlencode($var1),
"&var2=", urlencode($var2),
"')\">";
?>
The javascript function:
function popup(aURL)
{
alert(aURL);
window.open(aURL, '', 'width=500,height=250');
} // popup()
The alert() in the function is just to illustrate my point: When the alert comes up, the URL is not urlencoded anymore, as if javascript decoded it upon passing the argument to popup().
Is that indeed the case? Do all browsers do that or is it just IE? Actually how come it does that?
Thanks!
Cheers,
Cook
It's done since you get some things you might want to stick in an url as parameters which don't fit the spec for url parameters, which would make it impossible to figure out what the parameters were and what the parameter values were if just left like that and not encoded.
Thanks for your reply. I do understand what urlencoding is meant for, no problem about that.
My problem is more that I am surprised JS decodes a urlencoded function parameter. I mean it could be anything, a regular string. In my case it just happens to be a urlencoded string, but there's nothing that tell JS it is. What if I wanted it to not decode it?
Cheers,
Cook
If you've got any doubts as to whether they need to be decoded or not, why not slip a space or a quote in there and see what the result is? (my guess is that they should be encoded here).