简体   繁体   中英

passing multiple urls within url query params

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM