0

I have to handle a case where multiple urls should be passed in the url query params, for example:

http://example.com/?landing=<url1>&referrer=<url2>

url1: http://test1.com/test.html?param=x&param2=y
url2: http://test2.com/test/test.html?p=x&c=&...

At the moment I'm trying the following without success:

var l = encodeURIComponent(document.URL);
var r = encodeURIComponent(document.referrer);
window.location.href = 'http://example.net/?landing=' + l + '&ref=' + r;

My question is: What would be the best way to encode url1 and url2? Should I use encodeURIComponent or would it be better to use base64 encoding for those params or perhaps something else?

all jazz
  • 2,007
  • 2
  • 21
  • 37

1 Answers1

0

You're fine with URL-encoding the whole parameters, but if you're encoding full URLs you should use encodeURI instead of encodeURIComponent.

A possible issue here is that the first and/or second URL passed as query string parameters may reach the maximum characters allowed in an URL, but as long as you can predict that this will never happen, you're still fine again!

About the base64 approach, you could reach the so-called maximum characters allowed in an URL limitation easily.

Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206