Welcome to WebmasterWorld Guest from 54.198.41.76

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

urlencode and javascript

what's really going on?

     
1:20 am on Oct 15, 2004 (gmt 0)

New User

10+ Year Member

joined:Oct 14, 2004
posts:32
votes: 0


Hi,

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

8:18 am on Oct 15, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
posts:1001
votes: 0


urlencoding is a standard, so it's most likely to be the same amongst browsers. I've never done this with js, but I'd guess this must also be true for js.

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.

8:23 am on Oct 15, 2004 (gmt 0)

New User

10+ Year Member

joined:Oct 14, 2004
posts:32
votes: 0


Hi mincklerstraat,

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

1:00 pm on Oct 15, 2004 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12548
votes: 2


Did you try enclosing it in single quotes?
1:22 pm on Oct 15, 2004 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Mar 19, 2003
posts:1001
votes: 0


I guess what I'm saying is it's not necessarily 'js' knowing that it's a url encoded entity - the js makes a popup with a url in it, but it's then the browser that makes the request for the url. It'll request the url just as it requests any url - script.php plus the parameters, which are encoded, as they probably should be.

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).

4:13 pm on Oct 18, 2004 (gmt 0)

New User

10+ Year Member

joined:Oct 14, 2004
posts:32
votes: 0


Thanks for your help guys, it's quite clear now.
Cheers,
Cook