我必须处理一个案例,在url查询参数中应该传递多个url,例如:
http://example.com/?landing=<url1>&referrer=<url2>
url1: http://test1.com/test.html?param=x¶m2=y
url2: http://test2.com/test/test.html?p=x&c=&...
目前我没有成功尝试以下内容:
var l = encodeURIComponent(document.URL);
var r = encodeURIComponent(document.referrer);
window.location.href = 'http://example.net/?landing=' + l + '&ref=' + r;
我的问题是: 编码url1和url2的最佳方法是什么?我应该使用encodeURIComponent还是更好地为那些params或其他东西使用base64编码?
答案 0 :(得分:0)
您可以对所有参数进行网址编码,但如果您对完整网址进行编码,则应使用encodeURI
代替encodeURIComponent
。
这里可能存在的问题是,作为查询字符串参数传递的第一个和/或第二个URL可能会到达maximum characters allowed in an URL,但只要您可以预测这种情况永远不会发生,您仍然可以再次使用!
关于base64方法,您可以轻松达到URL限制中允许的所谓最大字符数。