Javascript 在url查询参数中传递多个url

Javascript 在url查询参数中传递多个url,javascript,uri,Javascript,Uri,我必须处理在url查询参数中应传递多个url的情况,例如: 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=&... 我的问题是: 对url1和url2进行编码的最佳方式是什么?我应该使用encod

我必须处理在url查询参数中应传递多个url的情况,例如:

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=&...
我的问题是:
对url1和url2进行编码的最佳方式是什么?我应该使用encodeURIComponent,还是对这些参数或其他参数使用base64编码更好?

您可以对整个参数使用URL编码,但如果您对完整URL进行编码,则应该使用encodeURI而不是encodeURIComponent

这里可能存在的一个问题是,作为查询字符串参数传递的第一个和/或第二个URL可能会到达,但只要您能够预测这种情况永远不会发生,您仍然可以再次使用

关于base64方法,您可以轻松达到URL限制中允许的所谓最大字符数

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