tags:

views:

117

answers:

3

Using '&url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

What is the recommended way to pass urls as url parameters ?

+1  A: 

Use escape() to url encode it, it will encode the ampersands so that does not happen.

Jeffrey Aylesworth
`escape` is similar to `encodeURIComponent`, but generates the wrong output for the `+` character, and any non-ASCII characters. It should never be used.
bobince
A: 

Using '&url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url

Yes, that's what you should be doing. encodeURIComponent is the correct way to encode a text value for putting in part of a query string.

but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.

Then the server is very broken indeed. If that's really what's happening, you need to fix it at the server end.

Code?

bobince
+2  A: 

encodeURIComponent() should work. For example,

'&url=' + encodeURIComponent("http://a.com/?q=query&n=10")

produces

"&url=http%3A%2F%2Fa.com%2F%3Fq%3Dquery%26n%3D10"

(which doesn't have any '&' or '?' in the value). When your server gets this url, it should be able to decode that to get the original:

param["url"] = "http://a.com/?q=query&n=10"

I'm not sure what server you're using (e.g. Rails, Django, ...) but that should work "out of the box" on any normal system.

Dustin Boswell