Skip to content
Advertisement

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

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 ?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement